通过注解来配置bean(2)

1 package com.yl.annotation.repository; 2 3 import org.springframework.beans.factory.annotation.Autowired; 4 import org.springframework.stereotype.Repository; 5 6 import com.yl.annotation.TestObject; 7 8 @Repository 9 //@Repository("userRepository") 10 public class UserRepositoryImpl implements UserRepository { 11 12 @Autowired(required=false) 13 private TestObject testObject; 14 15 @Override 16 public void save() { 17 System.out.println("UserRepository save..."); 18 System.out.println(testObject); 19 } 20 21 }

UserJdbcRepository.java

1 package com.yl.annotation.repository; 2 3 import org.springframework.stereotype.Repository; 4 5 @Repository 6 public class UserJdbcRepository implements UserRepository { 7 8 @Override 9 public void save() { 10 System.out.println("UserJdbcRepository save..."); 11 } 12 13 }

UserService.java

1 package com.yl.annotation.service; 2 3 import org.springframework.beans.factory.annotation.Autowired; 4 import org.springframework.beans.factory.annotation.Qualifier; 5 import org.springframework.stereotype.Service; 6 7 import com.yl.annotation.repository.UserRepository; 8 9 @Service 10 public class UserService { 11 @Autowired 12 @Qualifier("userJdbcRepository") 13 private UserRepository userRepository; 14 15 /*@Autowired 16 @Qualifier("userJdbcRepository") 17 public void setUserRepository(UserRepository userRepository) { 18 this.userRepository = userRepository; 19 }*/ 20 21 public void add() { 22 System.out.println("UserService add..."); 23 userRepository.save(); 24 } 25 }

UserController.java

1 package com.yl.annotation.controller; 2 3 import org.springframework.beans.factory.annotation.Autowired; 4 import org.springframework.stereotype.Controller; 5 6 import com.yl.annotation.service.UserService; 7 8 @Controller 9 public class UserController { 10 @Autowired 11 private UserService userService; 12 13 public void execute() { 14 System.out.println("UserController execute..."); 15 userService.add(); 16 } 17 }

beans-annotation.xml

1 <?xml version="1.0" encoding="UTF-8"?> 2 <beans xmlns="http://www.springframework.org/schema/beans" 3 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 4 xmlns:context="http://www.springframework.org/schema/context" 5 xsi:schemaLocation="http://www.springframework.org/schema/beans 6 http://www.springframework.org/schema/context/spring-context-4.1.xsd"> 7 8 <!-- 指定Spring IOC容器扫描的包 --> 9 <!-- 可以通过resource-pattern指定扫描的资源 --> 10 <!-- <context:component-scan 11 base-package="com.yl.annotation" 12 resource-pattern="repository/*.class"></context:component-scan> --> 13 14 <!-- context:exclude-filter 子节点指定排除哪些指定表达式的组件 --> 15 <!-- context:include-filter 子节点指定包含哪些指定表达式的组件, 该子节点需要use-default-filters配合使用 --> 16 <context:component-scan 17 base-package="com.yl.annotation" > 18 <!-- use-default-filters="false"> --> 19 <!-- <context:exclude-filter type="annotation" 20 expression="org.springframework.stereotype.Repository"/> --> 21 22 <!-- <context:include-filter type="annotation" 23 expression="org.springframework.stereotype.Repository"/> --> 24 25 <!-- <context:exclude-filter type="assignable" 26 expression="com.yl.annotation.repository.UserRepository"/> --> 27 28 <!-- <context:include-filter type="annotation" 29 expression="com.yl.annotation.repository.UserRepository"/> --> 30 </context:component-scan> 31 </beans>

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/c4981c60fe318a9e0f6ffbb423a702af.html