衍生注解
容器配置
User类
Userdao类
spring容器配置
创建测试类
将User类修改
修改spring容器配置
@Component使用
衍生注解在web开发中,会根据webmvc的三层加购分层,这时候每层都有相同一个不同的注解名字,但功能都相同。
Service层:@Service用于标注业务层组件
controller层:@Controller用于标注控制层组件
dao层:@Repository用于标注数据访问组件
组件归类层,名字可以随便定义,例如User层,@Component可以进行标注
注解:使用了注解之后,在容器中不需要注入bean,但要指定查找的的项目。
容器配置<?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.yc.ch"/>
</beans>
创建一个dao层和一个组件归类层,文件样式如下
User类import com.yc.dao.Userdao;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
//该注释表示注册到容器中,直接由spring容器托管,
@Component
public class User {
//表示设置name的参数值为为Tom
//等价于 <property name="name" value="Tom"/>
@Value("Tom")
private String name;
private Userdao userdao;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Userdao getUserdao() {
return userdao;
}
public void setUserdao(Userdao userdao) {
this.userdao = userdao;
}
@Override
public String toString() {
return "User{" +
"name='" + name + '\'' +
", userdao=" + userdao +
'}';
}
}
Userdao类
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Repository;
//该注释表示注册到容器中,直接由spring容器托管,
@Repository
public class Userdao {
//表示设置name的参数值为为游泳
//等价于 <property name="name" value="游泳"/>
@Value("游泳")
private String name;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@Override
public String toString() {
return "Userdao{" +
"name='" + name + '\'' +
'}';
}
}
spring容器配置
<?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">
<!--表示查找到com.yc下的所有层-->
<context:component-scan base-package="com.yc"/>
</beans>
创建测试类
ApplicationContext context=new ClassPathXmlApplicationContext("beans.xml");
User u=context.getBean("user",User.class);
System.out.println(u);
输出
User{name=‘Tom’, userdao=null}
最终输出的只有User类,但是Userdao层次却直接显示空,问题是没有把Userdao的bean引入到User的bean中,所以在测试User类时显示空。解决:使用使用注解 @Autowired方式匹配到Userdao中
将User类修改import com.yc.dao.Userdao;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
@Component
public class User {
@Value("Tom")
private String name;
@Autowired
private Userdao userdao;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Userdao getUserdao() {
return userdao;
}
public void setUserdao(Userdao userdao) {
this.userdao = userdao;
}
@Override
public String toString() {
return "User{" +
"name='" + name + '\'' +
", userdao=" + userdao +
'}';
}
}
修改spring容器配置
<?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.yc"/>
<context:annotation-config/>
</beans>
创建测试类,输出
@Component使用User{name=‘Tom’, userdao=Userdao{name=‘游泳’}}
@Component还可以加参数,里面值相当于注入bean中的id值,上面的案例默认为User类的小写user,如果设置为@Component(value = “user2”)或者@Component(“user2”),则将测试类改写为
ApplicationContext context=new ClassPathXmlApplicationContext("beans.xml");
User u=context.getBean("user2",User.class);
System.out.println(u);
以上为个人经验,希望能给大家一个参考,也希望大家多多支持易知道(ezd.cc)。