标签:
bean类:
package com.test; public class UserServiceImplement implements IUserService { private IUserDao user; public IUserDao getUser() { return user; } public void setUser(IUserDao user) { this.user = user; } public void saveUser() { user.addUser(); } }
Xml代码:
<?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:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:p="http://www.springframework.org/schema/p" xmlns:context="http://www.springframework.org/schema/context" xmlns:mvc="http://www.springframework.org/schema/mvc" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd"> <bean id="userdao" class="com.test.UserDaoImplement"/> <bean id="userservice" class="com.test.UserServiceImplement"> <property name="user" ref="userdao" /> </bean> </beans>
注:setter方式注入,对应的注入依靠属性,必须要有setter方法。
bean类:
package com.test; public class UserServiceImplement implements IUserService { private IUserDao user; int age; public UserServiceImplement(IUserDao user, int age) { this.user = user; this.age = age; } public void saveUser() { user.addUser(); System.out.println(this.age); } }
xml代码:
<?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:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:p="http://www.springframework.org/schema/p" xmlns:context="http://www.springframework.org/schema/context" xmlns:mvc="http://www.springframework.org/schema/mvc" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd"> <bean id="userdao" class="com.test.UserDaoImplement" /> <bean id="userservice" class="com.test.UserServiceImplement"> <constructor-arg index="0" type="com.test.IUserDao" ref="userdao"></constructor-arg> <constructor-arg index="1" value="24"></constructor-arg> </bean> </beans>
注:
二者区别:
@Resource注入方式代码:
package com.test; import javax.annotation.Resource; public class UserServiceImplement implements IUserService { @Resource private IUserDao user; private IUserDao user1; public IUserDao getUser1() { return user1; } @Resource public void setUser1(IUserDao user1) { this.user1 = user1; } public void saveUser() { user.addUser(); System.out.println("user注入成功"); user1.addUser(); System.out.println("user1注入成功"); } }
xml文件:
<?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:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:p="http://www.springframework.org/schema/p" xmlns:context="http://www.springframework.org/schema/context" xmlns:mvc="http://www.springframework.org/schema/mvc" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd"> <context:annotation-config /> <bean id="user" class="com.test.UserDaoImplement"/> <bean id="user1" class="com.test.UserDaoImplement"/> <bean id="userservice" class="com.test.UserServiceImplement"/> </beans>
@Autowired注入方式代码:
package com.test; import org.springframework.beans.factory.annotation.Autowired; public class UserServiceImplement implements IUserService { @Autowired private IUserDao user; public IUserDao getUser() { return user; } public void setUser(IUserDao user) { this.user = user; } public void saveUser() { user.addUser(); System.out.println("user注入成功"); } }
xml文件:
<?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:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:p="http://www.springframework.org/schema/p" xmlns:context="http://www.springframework.org/schema/context" xmlns:mvc="http://www.springframework.org/schema/mvc" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd"> <context:annotation-config /> <bean id="user" class="com.test.UserDaoImplement"/> <bean id="userservice" class="com.test.UserServiceImplement"/> </beans>
标签:
原文地址:http://www.cnblogs.com/dmir/p/4876125.html