Spring中Bean的配置(三)基于注解方式配置Bean

1.基于注解的方式配置Bean,裝配屬性
在classpath中扫描组件
1.组件扫描(component scanning):Spring能够从classpath下自动扫描,侦测和实例化具有特定注解的组件
2.特定组件包括
	1.@Component 基本注解 表示了一个受Spring管理的组件
	2.@Respositroy 标识持久层(Dao层 接口+实现类)组件
	3.@Service 标识服务层(业务层)组件
	4.@Controller 标识表现层(控制层)组件
3.对于扫描到的组件,Spring有默认的命名策略:使用非限定类名,第一个字母小写,也可以在注解中通过value属性值标识组件的名称,例如:"testObject"

eg
1.基本注解

@Component
public class TestObject {}
2.控制器层
@Controller
public class UserController {

	public void execute() {
		System.out.println("UserController……");
	}
}

3.持久层(接口+实现类)
1.接口

public interface UserRepository {
	
	void save();
}

2.实现类

@Repository("userRepository")//重命名
public class UserRepositoryImpl implements UserRepository {

	@Override
	public void save() {
		// TODO Auto-generated method stub
		System.out.println("UserRepositoryImpl……");
	}
}

5.服务层

@Service
public class UserService {
	
	public void add() {
		System.out.println("UserService add……");
	}
}

6.配置annotationBeans.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: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.3.xsd">

	<context:component-scan base-package="org.spring.annotation.component"></context:component-scan>
	
</beans>

7.测试类

public class Test5 {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		ApplicationContext ctx = new ClassPathXmlApplicationContext("annotationBeans.xml");
	
		TestObject to = (TestObject) ctx.getBean("testObject");
		System.out.println("testObject"+to);
		
		UserController userController = (UserController) ctx.getBean("userController");
		System.out.println("userController"+userController);
		
		UserRepository userRepository = (UserRepository) ctx.getBean("userRepositoryImpl");
		System.out.println("userRepository:"+userRepository);
		
		UserService userService = (UserService) ctx.getBean("userService");
		System.out.println("userService:"+userService);

	}

}
2.当在组件类上使用了特定的注解之后,还需要在Spring的配置文件中声明context:component-scan:
	1.base-package属性指定一个需要扫描的基类包,Spring容器将会扫描这个基类包里及其子类包中的所有类
	2.当选要扫描多个包时,可以使用逗号分隔。
	3.如果仅希望扫描特定的类而非基类包下的所有类,可使用rescue=pattern属性过滤特定的类,示例:
		1.<context:include-filter>子节点表示要包含的目标类
		2.<context:exclude-filter>子节点表示要排除在外的目标类
	4.<context:component-scan>下可以拥有若干个<context:include-filter>和<context:exclude-filter>子节点
3.context:include-filter和context:exclude-filter子节点支持多种类型的过滤表达式
	1.类型:annotation ;示例:com.atguigu.XxxAnnotation;说明:所有标注了XxxAnnotation 类。该类型采用目标类是否标注了某个注解进行过滤
	2.类型:assinable ;示例:com.atguigu.XxxService;说明:所有继承或扩展XxxService的类。该类型采用目标类是否继承或扩展某个特定类进行过滤
	3.类型:aspectj ;示例:com.atguigu..*Service+;说明:所有类名以Service结束的类及继承或扩展它们的类。该类型采用AspectJ表达式进行过滤
	4.类型:regex;示例:com.\atguigu\.anno\..* 说明所有com.atguigu.anno包下的类。该类型采用正则表达式根据类名进行过滤
	5.类型:custom;示例:com.atguigu.XxxTypeFilter 说明:采用XxxTypeFilter通过代码的方式定义过滤的规则。则该类必须实现org.springframework.core.type.TypeFilter接口

eg
1.配置annotationBeans.xml排除Repository(持久层)org.springframework.stereotype.Repository注解类包

<?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.3.xsd">

	<context:component-scan base-package="org.spring.annotation.component" resource-pattern="**/*.class">
		<context:exclude-filter type="annotation" expression="org.springframework.stereotype.Repository"/>
	</context:component-scan> 
</beans>

2.配置annotationBeans.xml只包含Repository(持久层)use-default-filters=“false” 不使用默认都关联

<?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.3.xsd">

	<context:component-scan base-package="org.spring.annotation.component" resource-pattern="**/*.class" use-default-filters="false">
		<context:include-filter type="annotation" expression="org.springframework.stereotype.Repository"/> 
	</context:component-scan> 
		
</beans>

3.assignable使用 排除注解 通过类名

<?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.3.xsd">
	
	<context:component-scan base-package="org.spring.annotation.component" resource-pattern="**/*.class">
		<context:exclude-filter type="assignable" expression="org.spring.annotation.component.UserRepository"/> 
	</context:component-scan>
	
</beans>

4.assignable使用 只包含注解 通过类名

<?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.3.xsd">
	
	<!-- <context:component-scan base-package="org.spring.annotation.component" resource-pattern="**/*.class" use-default-filters="false">
		<context:include-filter type="assignable" expression="org.spring.annotation.component.UserRepository"/>
	</context:component-scan>

</beans>
5.组件装配
	1.<context:component-scan>元素还会自动注册AutowiredAnnotationBeanPostProcessor实例,该实例可以自动装配具有@Autowired @Resource @Inject注解的属性
	7.使用@Autowired自动装配Bean
1.@Autowired 注解自动装配具有兼容类型的单个Bean属性
	1.构造器,普通字段(即使是非public,一切具有参数的方法都可以应用@Autowired注解
	2.默认情况下,所有使用@Autowired注解的属性都需要被设置,当Spring找不到匹配的Bean装配属性时,会抛出异常,若某一属性允许不被设置,可以设置@Autowired注解的required属性为false
	3.默认情况下,当IOC容器里存在多个类型兼容的Bean时,通过类型的自动装配将无法工作,此时可以在@Qualifier注解里提供Bean的名称,Spring允许对方法的入参标注@Qualifiter已指定注入Bean的名称
	4.@Autowired注解也可以应用在数组类型的属性上,此时Spring将会把所有匹配的Bean进行自动装配
	5.@Autowired注解也可以应用在集合属性上,此时Spring读取该集合的类型信息,然后自动装配所有与之兼容的Bean
	6.@Autowired注解用在java.util.Map上时,若该Map的键值为Spring,那么Spring将自动装配与之Map值类型兼容的Bean,此时Bean的名称作为键值

Autowired默认先按byType,如果发现找到多个bean,则又按照ByName方式比对,如果还有多个,则抛出异常

eg
1.控制层 @Autowired注解后的属性可以不写getter setter

@Controller
public class UserController {

	@Autowired
	private UserService userService;
	
	public void execute() {
		System.out.println("UserController");
		userService.add();
	}
}

2.服务层

@Service
public class UserService {

	@Autowired
	private UserRepository userRepository;
	
	public void add() {
		System.out.println("UserService");
		userRepository.save();
	}
}
@Repository("userRepository")
public class UserRepositoryImpl implements UserRepository {

	@Autowired
	private TestObject testObject;
	
	@Override
	public void save() {
		// TODO Auto-generated method stub
		System.out.println("UserRepositoryImpl");
		System.out.println("testObject:"+testObject);
	}

}

4.配置annotationBeans.xml

<context:component-scan base-package="org.spring.annotation.component"></context:component-scan>

5.测试类

public static void main(String[] args) {
	// TODO Auto-generated method stub
	ApplicationContext ctx = new ClassPathXmlApplicationContext("annotationBeans.xml");		
	UserController userController = (UserController) ctx.getBean("userController");
	userController.execute();
}
注入时切换名字

eg
1.新建UserRepositoryImpl2.java

@Repository("userRepository2")
public class UserRepositoryImpl2 implements UserRepository {

	@Autowired(required = false)
	private TestObject testObject;
	
	@Override
	public void save() {
		// TODO Auto-generated method stub
		System.out.println("UserRepositoryImpl22222");
		System.out.println("testObject:"+testObject);
	}

}
@Service
public class UserService {

	@Autowired
	@Qualifier("userRepositoryImpl2")
	private UserRepository userRepository;
	
	public void add() {
		System.out.println("UserService");
		userRepository.save();
	}
}
测试类会执行2
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Spring,我们可以使用注解配置和装配Bean,这可以使我们的代码更加简洁和易于维护。下面是关于如何基于注解配置和装配Bean的一些简要介绍: 1. 基于注解配置BeanSpring,我们可以使用以下注解配置Bean: - @Component:表示该类是一个Spring Bean,需要被Spring容器管理。 - @Service:表示该类是一个服务层的Bean。 - @Controller:表示该类是一个控制层的Bean。 - @Repository:表示该类是一个数据访问层的Bean。 这些注解都是基于@Component注解的衍生注解,它们的作用是更加明确地表示Bean的角色。我们可以在Bean类上添加这些注解,告诉Spring容器该类需要被管理。例如: ``` @Service public class UserService { // ... } ``` 2. 基于注解装配BeanSpring,我们可以使用以下注解来装配Bean: - @Autowired:自动装配Bean。 - @Qualifier:指定具体的Bean名称进行装配。 - @Resource:指定具体的Bean名称进行装配,与@Qualifier类似。 - @Value:注入一个具体的值。 使用@Autowired注解进行自动装配时,Spring会自动在容器寻找与该类型匹配的Bean,并将其注入到类的属性。例如: ``` @Service public class UserService { @Autowired private UserDao userDao; // ... } ``` 使用@Qualifier或@Resource注解可以指定具体的Bean名称进行装配。例如: ``` @Service public class UserService { @Autowired @Qualifier("userDaoImpl") private UserDao userDao; // ... } ``` 使用@Value注解可以注入一个具体的值。例如: ``` @Service public class UserService { @Value("10") private int maxCount; // ... } ``` 以上就是关于Spring基于注解配置和装配Bean的简要介绍,希望能对您有所帮助。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值