Bean于Bean之间存在关联关系情况下使用注解
<?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.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd"> <!-- 指定Spring IOC容器扫描的包 --> <!-- resource-pattern 扫描指定的资源 --> <!-- <context:component-scan base-package="com.test.spring.annotation" resource-pattern="controller/*.class" ></context:component-scan>--> <!-- exclude-filter 子节点指定排除那些指定表达式的组件 --> <!-- include-filter 子节点包含那些表达式的组件,需要配置 use-default-filters为false--> <context:component-scan base-package="com.test.spring.annotation" > <!-- <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/> <context:include-filter type="annotation" expression="org.springframework.stereotype.Controller"/> --> <!-- - <context:exclude-filter type="assignable" expression="com.test.spring.annotation.TestObject"/> <context:include-filter type="assignable" expression="com.test.spring.annotation.TestObject"/> --> </context:component-scan> </beans>
public static void main(String[] args) { ApplicationContext con=new ClassPathXmlApplicationContext("beans_annocation.xml"); // TestObject to=(TestObject) con.getBean("testObject"); // System.out.println(to); // // UserService ser=(UserService) con.getBean("userService"); // System.out.println(ser); // // UserController ucon=(UserController) con.getBean("userController"); System.out.println(ucon); ucon.execute(); // UserRepositoryImlpl ur=(UserRepositoryImlpl) con.getBean("userRepository"); // System.out.println(ur); }
package com.test.spring.annotation.controller; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Controller; import com.test.spring.annotation.service.UserService; @Controller public class UserController { @Autowired private UserService userservice; //@Autowired可以放在有参数的方法上 public void setUserservice(UserService userservice) { this.userservice = userservice; } public void execute(){ System.out.println("usercontroller"); userservice.add(); } }
package com.test.spring.annotation.service; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Qualifier; import org.springframework.stereotype.Service; import com.test.spring.annotation.repository.UserRepository; @Service public class UserService { @Autowired @Qualifier("userRepository")//指定装配那一个bean private UserRepository userRepository; //可以卸载参数前面 public void setUserRepository(@Qualifier("userRepository")UserRepository userRepository) { this.userRepository = userRepository; } public void add(){ System.out.println("USERSERVICE的 add"); userRepository.save(); } }
package com.test.spring.annotation.repository; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Repository; import com.test.spring.annotation.TestObject; //注解可以通过value属性值标识,bean在IOC容器中的name @Repository("userRepository") public class UserRepositoryImlpl implements UserRepository{ @Autowired(required=false) private TestObject to; public void save() { System.out.println("实现接口的save"); } }
原文:http://www.cnblogs.com/img-zoom/p/4570427.html