spring 注解开发
一、环境
1、导入包 maven
2、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="com.wt.pojo"/> <context:annotation-config/> </beans>
二、具体实现
1、bean
@Component
public class User {
}
2、属性注入
package com.wt.pojo; import org.springframework.beans.factory.annotation.Value; import org.springframework.stereotype.Component; @Component public class User { @Value("wt") private String name; public String getName() { return name; } public void setName(String name) { this.name = name; } }
3、衍生注解
dao [@Repository]
service [@Service]
controller[@Controller]
将类注入到spring容器中
4、自动装配注解
5、作用域注解
@Scope