标签:设置 his 维护 自己 tor ati setname git str
<?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:annotation-config/>
</beans>
<context:component-scan base-package="com.saxon.pojo"/>
<?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.saxon.pojo"/>
<!--开启注解的支持-->
<context:annotation-config/>
</beans>
@Component //等价于 <bean id="user" class="com.saxon.pojo.User"/>
@Data
public class User {
private String name;
}
@Value 添加在字段上或者set方法上,设置属性的值
@Component //等价于 <bean id="user" class="com.saxon.pojo.User"/>
@Data
public class User {
//等价于<property name="name" value="kuangshen" />
@Value("saxon")
private String name;
}
@Component //等价于 <bean id="user" class="com.saxon.pojo.User"/>
@Data
//id默认为pojo类的小写
public class User {
//等价于<property name="name" value="kuangshen" />
@Value("saxon")
private String name;
public void setName(String name) {
this.name = name;
}
}
@Component有几个衍生注解,我们在web开发中,会按照mvc三层架构分层
这4个注解功能都是一样的,都是代表某个类注册到Spring容器中,自动装配
@Scope(“singleton”)
添加到类上 设置为单例模式
xml与注解:
xml与注解 最佳实践:
我们在使用的过程中,只需要注意一个问题,必须让注解生效就需要开启注解的支持
<!--开启注解的支持-->
<context:annotation-config/>
<!--指定要扫描的包,这个包下的注解就会生效-->
<context:component-scan base-package="com.dada.*"/>
标签:设置 his 维护 自己 tor ati setname git str
原文地址:https://www.cnblogs.com/saxonsong/p/14929443.html