1.简单属性注入,直接在xml中给变量赋值,这种方法很少使用,不过在数据库连接时有时会用
<bean id="myDateSource" class="org.apache.commom.dbcp.MydateSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver"/>
<property name="url" value="jdbc:mysql://localhost:3306/mydb"/>
<property name="username" value="admin"/>
<property name="password" value="123456"/>
</bean>
2.bean的生命周期scope
在配置文件xml中可以配置bean的一个属性,即scope
<bean id="" class="" scope="">
从文档我得知scope的值:singleton,prototype,request.session,global session,其中默认人singleton,后面三种比较少使用,
下面验证singleton和prototype的区别
UserServiceTest.java
package com.dong.test;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.dong.service.UserService;
public class UserServiceTest {
@Test
public void testAdd() {
ApplicationContext applicationContext=new ClassPathXmlApplicationContext("beans.xml");
UserService userService=(UserService)applicationContext.getBean("userService");
UserService userService1=(UserService)applicationContext.getBean("userService");
System.out.println(userService==userService1);//根据输出的结果观察区别
}
}
然后再beans.xml中配置
<bean id="userService" class="com.dong.service.UserService" scope="singleton">,运行看输出结果 true
<bean id="userService" class="com.dong.service.UserService" scope="prototype">,运行看输出结果 false
从上面可以发现如果是singleton,对象是不变的,而prototype是变化的
补充:当bean中id是action时,则scope为prototype
3.自动装配autowire,比较常用的byName和byType
UserDAOImpl.java,重写一下toString
package com.dong.impl;
import com.dong.dao.UserDAO;
import com.dong.model.User;
public class UserDAOImpl implements UserDAO{
private String daoId;
public void save(User u){
System.out.println("haha");
}
@Override
public String toString(){
return daoId;
}
public String getDaoId() {
return daoId;
}
public void setDaoId(String daoId) {
this.daoId = daoId;
}
}
UserService.java,此处需注意一定别把构造方法写进去,我因为这个项目是把上午的项目拿过来,没改,后来试了半天没出来结果
package com.dong.service;
import com.dong.dao.UserDAO;
import com.dong.impl.UserDAOImpl;
import com.dong.model.User;
public class UserService {
private UserDAO userDAO=new UserDAOImpl();
//增加用户
public void add(User u){
this.userDAO.save(u);
}
public UserDAO getUserDAO() {
return userDAO;
}
public void setUserDAO(UserDAO userDAO) {
this.userDAO = userDAO;
}
}
UserServiceTest.java
package com.dong.test;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.dong.service.UserService;
public class UserServiceTest {
@Test
public void testAdd() {
ApplicationContext applicationContext=new ClassPathXmlApplicationContext("beans.xml");
UserService userService=(UserService)applicationContext.getBean("userService");
System.out.println(userService.getUserDAO());
}
}
把代码写好了后接着配置xml。
<bean id="userDAO1" class="com.dong.impl.UserDAOImpl">
<property name="daoId" value="1"></property>
</bean>
<bean id="userDAO2" class="com.dong.impl.UserDAOImpl">
<property name="daoId" value="2"></property>
</bean>
<bean id="userService" class="com.dong.service.UserService" autowire="byName">
</bean>
输出结果:null
<bean id="userDAO" class="com.dong.impl.UserDAOImpl">
<property name="daoId" value="1"></property>
</bean>
<bean id="userDAO2" class="com.dong.impl.UserDAOImpl">
<property name="daoId" value="2"></property>
</bean>
<bean id="userService" class="com.dong.service.UserService" autowire="byName">
</bean>
输出结果:1
<bean id="userDAO1" class="com.dong.impl.UserDAOImpl">
<property name="daoId" value="1"></property>
</bean>
<bean id="userDAO2" class="com.dong.impl.UserDAOImpl">
<property name="daoId" value="2"></property>
</bean>
<bean id="userService" class="com.dong.service.UserService" autowire="byType">
</bean>
报错
<bean id="userDAO2" class="com.dong.impl.UserDAOImpl">
<property name="daoId" value="2"></property>
</bean>
<bean id="userService" class="com.dong.service.UserService" autowire="byType">
</bean>
输出结果:2
由上可知,当autowire为byName时,bean中的id必须UserService.java中的变量名一致,因为它是通过setter和getter方法获取的;而当autowire为byTypee时,则id名只要与后台模糊匹配即可,<bean id="DAO" class="com.dong.impl.UserDAOImpl">甚至都可以,但只能是一个bean,否则报错 。
autowire也可以设置成全局的,即在<beans.... default-autowire="byName">中配置就可以了,则在<bean>中就是byName装配,当中<bean>也设置了autowire,则为<bean>中自己的类型装配
4.lazy_init,中spring使用是所有的bean都会初始化一次,如果实在是太多太多的话也许初始化会非常的慢,而有些bean有时用不上它也初始化了,所以可以使用lazy_init将其设置为<bean lazy_init="true">,也可以再<beans>中设置全局
5.init_method和destroy_method这个类似于servlet
先在UserService.java中添加者两个方法
public void init(){
System.out.println("init");
}
public void destroy(){
System.out.println("destroy");
}
接着在UserServiceTest.java中改为
public void testAdd() {
ClassPathXmlApplicationContext applicationContext=new ClassPathXmlApplicationContext("beans.xml");
UserService userService=(UserService)applicationContext.getBean("userService");
UserService userService1=(UserService)applicationContext.getBean("userService");
applicationContext.destroy();
}
最后配置xml文件,
<bean id="userService" class="com.dong.service.UserService" autowire="byType" init-method="init" destroy-method="destroy">
运行后会输出 init destroy
如果我中bean中设置scope为prototype,运行会发现输出 init init,并没有调用destroy,这是因为ApplicationContext是不会监控prototype的.所以建议init_method和destroy_method不要和prototype一起使用