标签:c style class blog code java
概要:
再classpath中扫描组件
package com.coslay.beans.annotation; import org.springframework.stereotype.Component; public class TestObject { }
package com.coslay.beans.annotation.controller; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Controller; import com.coslay.beans.annotation.service.UserService; @Controller public class UserController { @Autowired private UserService userService; public void execute(){ System.out.println("UserController execute..."); userService.add(); } }
package com.coslay.beans.annotation.service; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Qualifier; import org.springframework.stereotype.Repository; import org.springframework.stereotype.Service; import com.coslay.beans.annotation.reporsitory.UserRepository; @Service public class UserService { private UserRepository userRepository; //如果有多个类型匹配时,则会去匹配与这个属性名字相同的Bean的名字 //@Repository("userRepository") //public class UserRepositoryImpl implements UserRepository{ //因为名字相同所以匹配成功 //可以将@Autowired放在字段或者方法上 //如果没有指定名字 //还有一种方法是使用@Qualifier("userRepositoryImpl") //用来指定哪一个指定名字的Bean //可以将该标签放在字段,设置方法或者设置方法的传入参数前面 @Autowired @Qualifier("userRepositoryImpl") public void setUserRepository(UserRepository userRepository){ this.userRepository = userRepository; } // @Autowired // public void setUserRepository(@Qualifier("userRepositoryImpl") UserRepository userRepository){ // this.userRepository = userRepository; // } public void add(){ System.out.println("UserService add..."); userRepository.sava(); } }
package com.coslay.beans.annotation.reporsitory; public interface UserRepository { void sava(); }
package com.coslay.beans.annotation.reporsitory; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Repository; import com.coslay.beans.annotation.TestObject; @Repository("userRepository") public class UserRepositoryImpl implements UserRepository{ @Autowired(required=false) private TestObject testObject; @Override public void sava() { System.out.println("UserRepository Save..."); System.out.println(testObject); } }
package com.coslay.beans.annotation.reporsitory; import org.springframework.stereotype.Repository; @Repository public class UserJdbcRepository implements UserRepository { @Override public void sava() { System.out.println("UserJdbcRepository sava..."); } }
package com.coslay.beans.annotation; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; import com.coslay.beans.annotation.controller.UserController; import com.coslay.beans.annotation.reporsitory.UserRepository; import com.coslay.beans.annotation.service.UserService; public class Main { public static void main(String[] args) { ApplicationContext ctx = new ClassPathXmlApplicationContext("beans-annotation.xml"); // TestObject to = (TestObject) ctx.getBean("testObject"); // System.out.println(to); // UserController userController = (UserController) ctx.getBean("userController"); System.out.println(userController); userController.execute(); // // UserService userService = (UserService) ctx.getBean("userService"); // System.out.println(userService); // // UserRepository userRepository = (UserRepository) ctx.getBean("userRepository"); // System.out.println(userRepository); } }
<?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.coslay.beans.annotation" resource-pattern="reporsitory/*.class"> </context:component-scan> --> <!-- context:exclude-filter 子节点指定排除哪些指定表达式的组件 context:include-filter 子节点指定包含哪些表达式的组件,该子节点需要use-default-filters="false"配合使用(如果use-default-filters="ture"则使用系统默认扫描所有带有@Component@Controller@Service@Repository的组件) --> <context:component-scan base-package="com.coslay.beans.annotation"> <!-- <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Repository"/> --> <!-- <context:include-filter type="annotation" expression="org.springframework.stereotype.Repository"/> --> <!-- <context:exclude-filter type="assignable" expression="com.coslay.beans.annotation.reporsitory.UserRepository"/> --> <!-- <context:include-filter type="assignable" expression="com.coslay.beans.annotation.reporsitory.UserRepository"/> --> </context:component-scan> </beans>
spring 注解方式配置Bean,布布扣,bubuko.com
标签:c style class blog code java
原文地址:http://blog.csdn.net/coslay/article/details/28321165