spring除了XML的配置,还可以用Annotation注解来实现spring的配置功能,下面来介绍里面一些配置方法
1.Annotation第一步
a)修改xml文件,参考文档<context:annotation-config/><?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:context="http://www.springframework.org/schema/context" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd"> <context:annotation-config/> </beans>
<?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:context="http://www.springframework.org/schema/context" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd"> <context:annotation-config/> <bean id="u" class="cn.edu.hpu.dao.Impl.UserDaoImpl"> <property name="daoId" value="1"></property> </bean> <bean id="userService" class="cn.edu.hpu.service.UserService"> </bean> </beans>
package cn.edu.hpu.service; import org.springframework.beans.factory.annotation.Autowired; import cn.edu.hpu.dao.UserDao; import cn.edu.hpu.dao.Impl.UserDaoImpl; import cn.edu.hpu.model.User; public class UserService { private UserDao userDao; public void init(){ System.out.println("init"); } public UserDao getUserDao() { return userDao; } @Autowired public void setUserDao(UserDao userDao) { this.userDao = userDao; } public void add(User u){ this.userDao.save(u); } public void destroy(){ System.out.println("destroy"); } }
package cn.edu.hpu.service; import org.junit.Test; import org.springframework.beans.factory.BeanFactory; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; import cn.edu.hpu.dao.UserDao; import cn.edu.hpu.model.User; public class UserServiceTest { @Test public void testAdd() throws Exception{ ClassPathXmlApplicationContext ctx=new ClassPathXmlApplicationContext("beans.xml"); UserService userService=(UserService)ctx.getBean("userService"); System.out.println(userService.getUserDao()); ctx.destroy(); } }
package cn.edu.hpu.dao.Impl; import java.util.List; import java.util.Map; import java.util.Set; import cn.edu.hpu.dao.UserDao; import cn.edu.hpu.model.User; public class UserDaoImpl implements UserDao{ private int daoId; public int getDaoId() { return daoId; } public void setDaoId(int daoId) { this.daoId = daoId; } public void save(User u) { System.out.println("add success!!"); } @Override public String toString() { return "daoid="+daoId; } }
<?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:context="http://www.springframework.org/schema/context" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd"> <context:annotation-config/> <bean id="u" class="cn.edu.hpu.dao.Impl.UserDaoImpl"> <property name="daoId" value="1"></property> </bean> <bean id="u2" class="cn.edu.hpu.dao.Impl.UserDaoImpl"> <property name="daoId" value="2"></property> </bean> <bean id="userService" class="cn.edu.hpu.service.UserService"> </bean> </beans>
package cn.edu.hpu.service; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Qualifier; import cn.edu.hpu.dao.UserDao; import cn.edu.hpu.dao.Impl.UserDaoImpl; import cn.edu.hpu.model.User; public class UserService { private UserDao userDao; public void init(){ System.out.println("init"); } public UserDao getUserDao() { return userDao; } @Autowired public void setUserDao(@Qualifier("u2")UserDao userDao) { this.userDao = userDao; } public void add(User u){ this.userDao.save(u); } public void destroy(){ System.out.println("destroy"); } }
package cn.edu.hpu.service; import org.junit.Test; import org.springframework.beans.factory.BeanFactory; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; import cn.edu.hpu.dao.UserDao; import cn.edu.hpu.model.User; public class UserServiceTest { @Test public void testAdd() throws Exception{ ClassPathXmlApplicationContext ctx=new ClassPathXmlApplicationContext("beans.xml"); UserService userService=(UserService)ctx.getBean("userService"); System.out.println(userService.getUserDao()); ctx.destroy(); } }结果:
@Scope("XXX")里面指定是用什么样的生命范围(singleton、prototype)
尊重开源精神,尊重劳动成果,转载请注明出处:http://blog.csdn.net/acmman/article/details/43889483