spring之自动装配bean
1、通过xml设置自动装配:
实体类:

xml文件:

autowire可改为:
autowire="byType"
2、通过注解方式实现自动装配:
先加入约束:
xmlns:context="http://www.springframework.org/schema/context"
加上标签开启:
<context:annotation-config/>
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 https://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd"> <context:component-scan base-package="pojo"/> <context:annotation-config/> </beans>
解析:

实体类上的操作:

此外:
dao层使用:@Repository
service层使用:@Service
controller层使用:@Controller
进行自动托管到spring,功能和@Component一致。
3、纯java类进行配置:
核心配置类写法:

获取数据测试:


浙公网安备 33010602011771号