Spring 入門

Water_tu發表於2020-12-22

1、 新建資料夾learn_spring

2、在idea,File-Open ,開啟資料夾learn_spring

3、新建module, 取名為spring_ioc 

4、File-Project Structure,然後點選 +  ,選擇web,選擇要新增的模組,然後修改裡面的路徑

工程新建完成

5、修改pom.xml, 導包

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>org.example</groupId>
    <artifactId>spring_ioc</artifactId>
    <version>1.0-SNAPSHOT</version>
    
<dependencies>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-context</artifactId>
        <version>5.0.5.RELEASE</version>
    </dependency>

    <dependency>
        <groupId>junit</groupId>
        <artifactId>junit</artifactId>
        <version>4.12</version>

    </dependency>
    <dependency>
        <groupId>junit</groupId>
        <artifactId>junit</artifactId>
        <version>RELEASE</version>
        <scope>test</scope>
    </dependency>

</dependencies>
    
</project>

6、在src/main/java下 新建包 com.tuy.dao.impl ,com.tuy.dao.demo  , com.tuy.dao.domain  , com.tuy.dao.service

7、編寫dao層程式碼

package com.tuy.dao.impl;

import com.tuy.dao.UserDao;

//dao層(持久層)
public class UserDaoImpl implements UserDao {


     public UserDaoImpl() {
        System.out.println("UserDaoImpl Create....");
    }

    public void init(){
        System.out.println("Init method....");
    }

    public void destory(){
        System.out.println("destory method....");
    }

    
    public void save() {
        System.out.println("save running...");
    }
}

 

package com.tuy.dao;

public interface UserDao {
    public void save();
}

9、編寫service層程式碼

package com.tuy.service.impl;

import com.tuy.dao.UserDao;
import com.tuy.domain.User;
import com.tuy.service.UserService;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import java.util.List;
import java.util.Map;
import java.util.Properties;

//業務層
public class UserServiceImpl implements UserService {


    private UserDao userDao;
    //需要把dao注入到service,需要在配置檔案 applicationContext.xml裡面配置
    public void setUserDao(UserDao userDao) {
        this.userDao = userDao;
    }

    public UserServiceImpl()
    {
    }

    public  UserServiceImpl(UserDao userDao)
    {
        this.userDao=userDao;
    }

    public void save() {
        userDao.save();

        System.out.println(username+","+age);
        System.out.println(strList);
        System.out.println(userMap);
        System.out.println(properties);
    }

//普通屬性
    private String username;
    private  int age;

    public void setUsername(String username) {
        this.username = username;
    }

    public void setAge(int age) {
        this.age = age;
    }

//集合
    private List<String> strList;
    private Map<String, User> userMap;
    private Properties properties;

    public void setStrList(List<String> strList) {
        this.strList = strList;
    }

    public void setUserMap(Map<String, User> userMap) {
        this.userMap = userMap;
    }

    public void setProperties(Properties properties) {
        this.properties = properties;
    }
}
package com.tuy.service;

public interface UserService {

    public void save();
}

10、 編寫User

package com.tuy.domain;

public class User {

    private  String name;
    private String addr;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getAddr() {
        return addr;
    }

    public void setAddr(String addr) {
        this.addr = addr;
    }

    @Override
    public String toString() {
        return "User{" +
                "name='" + name + '\'' +
                ", addr='" + addr + '\'' +
                '}';
    }
}

11、編寫web層(這裡是模擬的)

package com.tuy.demo;

import com.tuy.dao.UserDao;
import com.tuy.service.UserService;
import com.tuy.service.impl.UserServiceImpl;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

//UserController 模擬web層
public class UserController {

    public static void main(String[] args) {

        //ClassPathXmlApplicationContext 它是從類的根路徑下載入配置檔案 推薦使用這種, 在resource目錄下
        ApplicationContext app=new ClassPathXmlApplicationContext("applicationContext.xml");
        // 產生業務層的物件
       // UserService userService1 = (UserService) app.getBean("userService"); //app.getBean("id")
        UserService userService1 =  app.getBean(UserService.class);//不適合配置檔案有多個相同id
        userService1.save();

    }


}

12、編寫配置檔案applicationContext.xml,需要放在resources目錄下

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:p="http://www.springframework.org/schema/p"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">

    <!-- dao層(持久層)   -->
    <!--  init-method指定初始化方法,destroy-method指定物件銷燬方法   -->
    <!--  使用無參構造方法例項化 UserDaoImpl物件 -->
    <bean id="userDao" class="com.tuy.dao.impl.UserDaoImpl" scope="singleton" init-method="init" destroy-method="destory"></bean>

    <!-- 業務層-->
<!--    <bean id="userService" class="com.tuy.service.impl.UserServiceImpl"  >-->
<!--    &lt;!&ndash;  業務層依賴dao層,所以需要 把dao層物件注入到業務層      &ndash;&gt;-->
<!--    &lt;!&ndash;第一種方法  配置依賴注入, 配置屬性的名字,需要是在UserServiceImpl的setUserDao方法中的set後面的userDao(首字母改為小寫),     &ndash;&gt;-->
<!--    &lt;!&ndash;  ref的值是引用spring容器中bean的id      &ndash;&gt;-->
<!--        <property name="userDao" ref="userDao"></property>-->
<!--    </bean>-->

<!--____________-->
<!--    <bean id="userService" class="com.tuy.service.impl.UserServiceImpl"  >-->
    <!--第二種方法  通過有參構造方法來注入dao層依賴  -->
<!--        &lt;!&ndash;  配置依賴注入, name是構造引數名字, ref的值是引用spring容器中bean的id      &ndash;&gt;-->
<!--        &lt;!&ndash;  ref 後面是要注入(引用)的bean物件的id      &ndash;&gt;-->
<!--        <constructor-arg name="userDao" ref="userDao"></constructor-arg>-->
<!--    </bean>-->


    <!--____________-->
<!--    第三種方法 依賴注入的方法: 需要引入名稱空間  xmlns:p="http://www.springframework.org/schema/p" -->
        <!--  然後新增屬性 p:userDao-ref="userDao"    -->
<!--    <bean id="userService" class="com.tuy.service.impl.UserServiceImpl"  p:userDao-ref="userDao" />-->


    <bean id="user1" class="com.tuy.domain.User">
        <property name="name" value="ton"></property>
        <property name="addr" value="beijing"></property>
    </bean>
    <bean id="user2" class="com.tuy.domain.User">
        <property name="name" value="ton2"></property>
        <property name="addr" value="beijing2"></property>
    </bean>


    <bean id="userService" class="com.tuy.service.impl.UserServiceImpl"  >
        <!--  業務層依賴dao層,所以需要 把dao層物件注入到業務層      -->
        <!--第一種方法  配置依賴注入, 配置屬性的名字,需要是在UserServiceImpl的setUserDao方法中的set後面的userDao(首字母改為小寫),     -->
        <!--  ref的值是引用spring容器中bean的id      -->
        <property name="userDao" ref="userDao"></property>

        <!--普通屬性的注入-->
        <property name="username" value="zhangsan"></property>
        <property name="age" value="18"></property>

        <!--集合性的注入-->
        <!-- List-->
        <property name="strList">
            <list>
            <!--      集合裡面的資料型別是普通資料型別,用value,否則用ref          -->
                <value>hha</value>
                <value>cca</value>
            </list>
        </property>
        <!--Map-->
        <property name="userMap">
            <map>
                <entry key="user1" value-ref="user1"></entry>
                <entry key="user1" value-ref="user2"></entry>
            </map>
        </property>
<!--Properties-->
        <property name="properties">
            <props>
                <prop key="p1">pvaaa1</prop>
                <prop key="p2">pvaaa2</prop>
                <prop key="p3">pvaaa3</prop>
            </props>
        </property>


    </bean>


<!--    引入其他配置檔案,可以用來分模組開發-->
<!--<import resource="applicationContext-user.xml"/>-->



</beans>

 

 

 

 

相關文章