Spring(基础篇1)

本篇博客讲解Spring框架里面的通过xml方式对Bean的基础配置,实例化方式,以及DI的注入,偏于基础。

一.Bean的基础配置  

  • Id 属性 and Class 属性
 <bean id = "userService"  class="com.itheima.Service.Impl.UserServiceImpl"></bean>
  •  Scope属性
Scope属性取值含义
 singleton
单例,默认值,spring容器创建的时候,就进行了bean的实例化,并存储到容器内部的单例池中
 prototype
原型,Spring容器初始化的时候不会创建bean实例,当调用getBean时才会实例化Bean,每次getBean都会创建一个新的Bean实例。

 

 

 

 

例如:

  <bean id = "userService"  class="com.itheima.Service.Impl.UserServiceImpl" scope="prototype" > </bean>
  •  Lazy-init属性

          当lazy-init为true时延迟加载,也就是spring创建时不会创建bean,等待用到时,在创建bean实例并存到单例池。

例如:

 <bean id = "userService"  class="com.itheima.Service.Impl.UserServiceImpl" lazy-init="true" ></bean>
  • Init-method 属性 and Destroy-method 属性
属性名含义
Init-method指定Bean的初始化方法
Destroy-method指定Bean的销毁方法

 

 

例如: 

package com.itheima.Service.Impl;
import com.itheima.Service.UserService;
public class UserServiceImpl implements UserService {

    public void init() {
        System.out.println("初始化方法");
    }

    public void destroy() {
        System.out.println("销毁方法");
    }
}

配置:

  <bean id = "userService"  class="com.itheima.Service.Impl.UserServiceImpl" init-method="init" destroy-method="destroy" ></bean>

二. Bean的实例化方式

  • 无参构造器 
package com.itheima.Test;
/**
 * 无参构造方法创建Bean*/
public class UserTest {

    public UserTest() {

    }
}

配置:

 <bean id="UserTest" class="com.itheima.Test.UserTest"></bean>
  •  有参构造器 
package com.itheima.Test;
/**
 * 有参构造方法创建Bean*/
public class UserTest {
    private String  name;

    public UserTest(String name) {
        this.name = name;
    }
}

配置:

<bean id="UserTest" class="com.itheima.Test.UserTest">
        <constructor-arg name="name" value="xx"></constructor-arg></bean>
  • 静态工厂 

静态工厂:

package com.itheima.Test;
/**静态工厂*/
public class Test2 {
    public static UserTest2 userTest2(){
        return userTest2();
    }
}

需要被创建的Bean:

package com.itheima.Test;
public class UserTest2 {
}

配置:

<bean id="UserTest2" class="com.itheima.Test.Test2" factory-method="userTest2"></bean>

作用:

1. 配置第三方的bean放到bean容器中。

2.可以在创建bean之前执行一些操作。

  • 实例工厂 

 实例工厂:

package com.itheima.Test;
public class Test3 {
    public UserTest3 userTest3(){
        return new UserTest3();
    }
}

需要被创建的Bean:

package com.itheima.Test;
public class UserTest3 {
}

配置:

<bean id="Test3" class="com.itheima.Test.Test3"></bean>
    <bean id="UserTest3" factory-bean="Test3" factory-method="userTest3"></bean>

作用:

1. 配置第三方的bean放到bean容器中。

2.可以在创建bean之前执行一些操作。

  三. DI注入(均使用Set方法注入)

  • 普通数据类型注入 
package com.lsl.Controller.Impl;
import com.lsl.Controller.UserController;
public class UserControllerImpl implements UserController {

    private String UserName;
    public void setUserName(String userName) {
        UserName = userName;
    }

}

配置:

<bean id="UserController" class="com.lsl.Controller.Impl.UserControllerImpl">
 <property name="userName" value="lsl"></property>
</bean>
  • 对象引用类型注入 
package com.lsl.Dao.Impl;
import com.lsl.Controller.UserController;
import com.lsl.Dao.UserDao;

public class UserDaoImpl implements UserDao {
    private UserController userController;
    public void setUserController(UserController userController) {
        this.userController = userController;
    }
}

配置:

<bean id="UserDao" class="com.lsl.Dao.Impl.UserDaoImpl">
    <property name="userController" ref="UserController"></property>
</bean>
  • List集合 and Set集合
package com.lsl.Service.Impl;
import com.lsl.Service.UserService;
import java.util.List;
import java.util.Set;

public class UserServiceImpl implements UserService {
    private List<String> stringList;
    private Set<String> stringSet;
   
    public void setStringList(List<String> stringList) {
        this.stringList = stringList;
    }

    public void setStringSet(Set<String> stringSet) {
        this.stringSet = stringSet;
    }

}

配置:

<bean id="UserService" class="com.lsl.Service.Impl.UserServiceImpl">
 <property name="stringList">
     <list>
         <value>121</value>
         <value>bbb</value>
         <value>ccc</value>
     </list>
 </property>

    <property name="stringSet">
        <set>
            <value>234</value>
            <value>5678</value>
        </set>
    </property>
</bean>
  •  Map集合
package com.lsl.Service.Impl;
import com.lsl.Service.UserService;
import java.util.Map;

public class UserServiceImpl implements UserService {
    private Map<String,String> stringStringMap;

    public void setStringStringMap(Map<String, String> stringStringMap) {
        this.stringStringMap = stringStringMap;
    }
}

配置:

<bean id="UserService" class="com.lsl.Service.Impl.UserServiceImpl">
    <property name="stringStringMap">
        <map>
            <entry key="qq" value="11111"></entry>
            <entry key="qq" value="11111"></entry>
            <entry key="qq" value="11111"></entry>
        </map>
    </property>
</bean>
  • properties类型
package com.lsl.Controller.Impl;
import com.lsl.Controller.UserController;
import java.util.Properties;
public class UserControllerImpl implements UserController {
    
    private Properties properties;

    public void setProperties(Properties properties) {
        this.properties = properties;
    }
}

配置:

<bean id="UserController" class="com.lsl.Controller.Impl.UserControllerImpl">
    <property name="userName" value="lsl"></property>
    <property name="properties">
        <props>
            <prop key="p1">1111</prop>
            <prop key="p2">2222</prop>
        </props>
    </property>
</bean>
  • 拓展 

以上配置均为手动配置,除此之外Spring提供的有自动装配功能,可以根据类型或者名字进行自动装配(详解在基础第二篇)。


 

 

 

 

 

 

 

 

 

 

 

 

 

  • 8
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值