spring框架学习笔记2:配置详解

2018-06-18 03:13:46来源:未知 阅读 ()

新老客户大回馈,云服务器低至5折

简单写一个类做示例:

package bean;

public class User {
    private String name;
    private Integer age;
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public Integer getAge() {
        return age;
    }
    public void setAge(Integer age) {
        this.age = age;
    }
}
View Code

 

Bean元素:

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


    <!-- 将User对象交给spring容器管理 -->
    <!-- Bean元素:使用该元素描述需要spring容器管理的对象
            class属性:被管理对象的完整类名.
            name属性:给被管理的对象起个名字.获得对象时根据该名称获得对象.  
                    可以重复.可以使用特殊字符.
            id属性: 与name属性一模一样. 
                   名称不可重复.不能使用特殊字符.
            结论: 尽量使用name属性.
      -->
    <bean  name="user" class="bean.User" ></bean>
    <!-- 导入其他spring配置文件 -->
    <import resource="package/applicationContext.xml"/>
    
</beans>

 

对象创建的三种方式:

1.空参构造创建(推荐):

    <bean  name="user" class="bean.User" ></bean>

 

2.静态工厂方式创建:

package create;

import bean.User;

public class UserFactory {

    public static User createUser(){
        
        System.out.println("静态工厂创建User");
        
        return new User();
        
    }
}
    <!-- 创建方式2:静态工厂创建 
          调用UserFactory的createUser方法创建名为user2的对象.放入容器
     -->
    <bean  name="user2" 
        class="create.UserFactory" 
        factory-method="createUser" >
    </bean>

 

3.实例工厂方式创建:

package create;

import bean.User;

public class UserFactory {
    
    public User createUser2(){
        
        System.out.println("实例工厂创建User");
        
        return new User();
        
    }
    
}
    <!-- 创建方式3:实例工厂创建 
         调用UserFactory对象的createUser2方法创建名为user3的对象.放入容器
     -->
    <bean  name="user3" 
        factory-bean="userFactory"
        factory-method="createUser2" ></bean>
        
    <bean  name="userFactory" 
        class="create.UserFactory"   ></bean>

 

Bean元素的Scope属性:

scope:singleton 单例(默认):被标识为单例的对象在Spring容器中只会创建一个实例
scope:prototype 多例:被标识为多例的每次创建都会是一个新的对象

验证单例和多例:

 

                @Test
                //scope:singleton 单例
                //scope:prototype 多例
                public void fun4(){
                    //1 创建容器对象
                    ApplicationContext ac = new ClassPathXmlApplicationContext("create/applicationContext.xml");
                    //2 向容器"要"user对象
                    User u1 = (User) ac.getBean("user");
                    User u2 = (User) ac.getBean("user");
                    User u3 = (User) ac.getBean("user");
                    User u4 = (User) ac.getBean("user");
                    
                    System.out.println(u2==u4);//单例:true
                                               //多例:false
                    //3 打印user对象
                    System.out.println(u);
                }

 

使用场景:基本都是使用默认单例

但是,整合Struts2框架的时候,Action要配成多例,因为Struts2框架每次请求都会创建一个新的Action对象

 

 

生命周期属性(初始化,销毁):

添加方法:

package bean;

public class User {
    
    private String name;
    private Integer age;
        
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public Integer getAge() {
        return age;
    }
    public void setAge(Integer age) {
        this.age = age;
    }
    
    public void init(){
        System.out.println("初始化方法!");
    }
    public void destory(){
        System.out.println("销毁方法!");
    }
    
}
View Code

配置文件:

    <bean  name="user" class="bean.User"
         init-method="init" destroy-method="destory" ></bean>

测试:

                @Test
                //测试生命周期方法
                public void fun5(){
                    //1 创建容器对象
                    ClassPathXmlApplicationContext ac = new ClassPathXmlApplicationContext("create/applicationContext.xml");
                    //2 向容器"要"user对象
                    User u = (User) ac.getBean("user");
                    //3 打印user对象
                    System.out.println(u);
                    //关闭容器,触发销毁方法
                    ac.close();
                }

 

 

 

Spring属性注入:

1.set方法注入(推荐):

再创建一个实体类做示例:

package bean;

public class Car {
    private String  name;
    private String color;
    
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public String getColor() {
        return color;
    }
    public void setColor(String color) {
        this.color = color;
    }
    @Override
    public String toString() {
        return "Car [name=" + name + ", color=" + color + "]";
    }
    
    
}
View Code

修改User类:

package bean;

public class User {
    
    private String name;
    private Integer age;
    private Car car;
    
    public Car getCar() {
        return car;
    }
    public void setCar(Car car) {
        this.car = car;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public Integer getAge() {
        return age;
    }
    public void setAge(Integer age) {
        this.age = age;
    }
    
    public void init(){
        System.out.println("我是初始化方法!");
    }
    public void destory(){
        System.out.println("我是销毁方法!");
    }
    @Override
    public String toString() {
        return "User [name=" + name + ", age=" + age + ", car=" + car + "]";
    }
    
}
View Code

 

配置文件:

    <!-- set方式注入: -->
    <bean name="user" class="bean.User">
        <!--值类型注入: 为User对象中名为name的属性注入tom作为值 -->
        <property name="name" value="tom"></property>
        <property name="age" value="18"></property>
        <!-- 引用类型注入: 为car属性注入下方配置的car对象 -->
        <property name="car" ref="car"></property>
    </bean>
    <!-- 将car对象配置到容器中 -->
    <bean name="car" class="bean.Car">
        <property name="name" value="兰博基尼"></property>
        <property name="color" value="黄色"></property>
    </bean>

测试:

    @Test
    public void fun1(){
        //1 创建容器对象
        ApplicationContext ac = new ClassPathXmlApplicationContext("injection/applicationContext.xml");
        //2 向容器"要"user对象
        User u = (User) ac.getBean("user");
        //3 打印user对象
        System.out.println(u);
    }

 

构造函数注入:

给User加上构造方法:

package bean;

public class User {
    
    private String name;
    private Integer age;
    private Car car;
    
    
    
    public User(String name, Car car) {
        System.out.println("User(String name, Car car)!!");
        this.name = name;
        this.car = car;
    }
    
    public User(Car car,String name) {
        System.out.println("User(Car car,String name)!!");
        this.name = name;
        this.car = car;
    }
    
    public User(Integer name, Car car) {
        System.out.println("User(Integer name, Car car)!!");
        this.name = name+"";
        this.car = car;
    }
    
    public Car getCar() {
        return car;
    }
    public void setCar(Car car) {
        this.car = car;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public Integer getAge() {
        return age;
    }
    public void setAge(Integer age) {
        this.age = age;
    }
    
    public void init(){
        System.out.println("我是初始化方法!");
    }
    public void destory(){
        System.out.println("我是销毁方法!");
    }
    @Override
    public String toString() {
        return "User [name=" + name + ", age=" + age + ", car=" + car + "]";
    }
    
}
View Code

配置文件:

    <bean name="user2" class="bean.User">
        <!-- name属性: 构造函数的参数名 -->
        <!-- index属性: 构造函数的参数索引 -->
        <!-- type属性: 构造函数的参数类型 -->
        <constructor-arg name="name" index="0" type="java.lang.Integer" value="66"></constructor-arg>
        <constructor-arg name="car" ref="car" index="1"></constructor-arg>
    </bean>

 

复杂类型注入:

package injection;

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

public class CollectionBean {
    private Object[] arr;//数组类型注入
    private List list;//list/set 类型注入
    private Map map;//map类型注入
    private Properties prop;//properties类型注入
    
    public Object[] getArr() {
        return arr;
    }
    public void setArr(Object[] arr) {
        this.arr = arr;
    }
    public List getList() {
        return list;
    }
    public void setList(List list) {
        this.list = list;
    }
    public Map getMap() {
        return map;
    }
    public void setMap(Map map) {
        this.map = map;
    }
    public Properties getProp() {
        return prop;
    }
    public void setProp(Properties prop) {
        this.prop = prop;
    }
    @Override
    public String toString() {
        return "CollectionBean [arr=" + Arrays.toString(arr) + ", list=" + list + ", map=" + map + ", prop=" + prop
                + "]";
    }
    
    
}
View Code

 

配置文化:

    <!-- 复杂类型注入 -->
    <bean name="cb" class="injection.CollectionBean">
        <!-- 如果数组中只准备注入一个值(对象),直接使用value|ref即可 <property name="arr" value="tom"></property> -->
        <!-- array注入,多个元素注入 -->
        <property name="arr">
            <array>
                <value>tom</value>
                <value>jerry</value>
                <ref bean="user" />
            </array>
        </property>

        <property name="list">
            <list>
                <value>jack</value>
                <value>rose</value>
                <ref bean="user" />
            </list>
        </property>
        <!-- map类型注入 -->
        <property name="map">
            <map>
                <entry key="url" value="jdbc:mysql:///mybase"></entry>
                <entry key="user" value-ref="root"></entry>
                <entry key-ref="user" value-ref="user1"></entry>
            </map>
        </property>
        <!-- prperties 类型注入 -->
        <property name="prop">
            <props>
                <prop key="driverClass">com.jdbc.mysql.Driver</prop>
                <prop key="userName">root</prop>
                <prop key="password">1234</prop>
            </props>
        </property>
    </bean>

 

标签:

版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com
特别注意:本站所有转载文章言论不代表本站观点,本站所提供的摄影照片,插画,设计作品,如需使用,请与原作者联系,版权归原作者所有

上一篇:SpringBoot注解

下一篇:内部类