实体类:
1 package com.bxw.po; 2 3 public class User { 4 private String username; //账号 5 private String password; //密码 6 public String getUsername() { 7 return username; 8 } 9 public void setUsername(String username) { 10 this.username = username; 11 } 12 public String getPassword() { 13 return password; 14 } 15 public void setPassword(String password) { 16 this.password = password; 17 } 18 19 20 }
Dao:
1 package com.bxw.dao; 2 3 import com.bxw.po.User; 4 5 public interface UserDao { 6 public void save(User u); 7 }
DaoImpl:
1 package com.bxw.dao.impl; 2 3 import com.bxw.dao.UserDao; 4 import com.bxw.po.User; 5 6 public class UserDaoImpl implements UserDao { 7 public void save(User u){ 8 System.out.println("user saved"); 9 } 10 }
UserService:
1 package com.bxw.service; 2 3 import com.bxw.dao.UserDao; 4 import com.bxw.dao.impl.UserDaoImpl; 5 import com.bxw.po.User; 6 7 public class UserService { 8 private UserDao userDao = new UserDaoImpl(); 9 10 public UserDao getUserDao() { 11 return userDao; 12 } 13 14 public void setUserDao(UserDao userDao) { 15 this.userDao = userDao; 16 } 17 18 19 public void add(User u){ 20 this.userDao.save(u); 21 } 22 23 }
applicationContext.xml:
1 <?xml version="1.0" encoding="UTF-8"?> 2 <beans 3 xmlns="http://www.springframework.org/schema/beans" 4 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 5 xmlns:p="http://www.springframework.org/schema/p" 6 xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd"> 7 8 <bean id="userDao" class="com.bxw.dao.impl.UserDaoImpl" /> 9 <bean id="userService" class="com.bxw.service.UserService" autowire="byName" /> 10 11 </beans>
UserServiceTest.java:
1 package com.bxw.test; 2 3 import org.junit.Test; 4 import org.springframework.context.ApplicationContext; 5 import org.springframework.context.support.ClassPathXmlApplicationContext; 6 7 import com.bxw.po.User; 8 import com.bxw.service.UserService; 9 10 public class UserServiceTest { 11 12 @Test 13 public void testAdd() throws Exception{ 14 // BeanFactory ac = new ClassPathXMLApplicationContext(); //初始化所有bean 15 // UserService service = (UserService) ac.getBean("userService"); //拿到id为userService,依赖注入userDao 16 ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml"); 17 UserService service = (UserService) ac.getBean("userService"); 18 User u = new User(); 19 service.add(u); 20 } 21 }
autowire常见取值有byname,bytype(其余取值不常用)。如果取值为byName,则UserService会去找名为UserDao的bean;如果取值为byType,则UserService则会去找与UserDao类型相同的bean。如果取值为defaule,则效果与在beans中指定default-autowire指定的属性相同。
===============华丽的分界线==================
1 <?xml version="1.0" encoding="UTF-8"?> 2 <beans 3 xmlns="http://www.springframework.org/schema/beans" 4 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 5 xmlns:p="http://www.springframework.org/schema/p" 6 xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd"> 7 8 <bean id="userDao" class="com.bxw.dao.impl.UserDaoImpl" /> 9 <bean id="userService" class="com.bxw.service.UserService" autowire="byName" lazy-init="true" scope="singleton" init-method="init" destroy-method="destroy"/> 10 11 </beans>
lazy-init=true,则说明该bean在getBean时才初始化,如果在<beans>标签中指定default-lazy-init=ture则该beans中的所有bean都按照这种方式加载。反之在ClassPathXmlApplicationContext被new出来时加载。
scope=singleton,则说明该bean采用单例模式,即自始至终该bean只有一个,若scope=prototype,则该bean每次get时都有一个新的对象(其余不常用)。
init-method:该bean初始化时调用的方法。
destroy-method:destroy时调用的方法。