Spring注解开发
程序员文章站
2022-07-08 08:03:14
...
Spring注解
1.使用注解须知:
1.导入约束:
xmlns:context=“http://www.springframework.org/schema/context”
2.×××配置注解支持:
context:annotation-config/
最终:
<?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>
2.注解实现自动装配
使用Autowired:可以指定单个不重复的id,而如果要指定某个id时要使用@Qualifier(value = “xxx”)
public class People {
/*zhushi自动导入*/
@Autowired
private Cat cat;
@Qualifier(value = "dog2")
private Dog dog;
private String name;
<bean id="dog1" class="com.zz.pojo.Dog"/>
<bean id="dog2" class="com.zz.pojo.Dog"/>
<!--使用@Qualifier(value = "dog2")进行指定装配-->
<bean id="dog3" class="com.zz.pojo.Dog"/>
<bean id="cat" class="com.zz.pojo.Cat"/>
<bean id="cat2" class="com.zz.pojo.Cat"/>
<bean id="people" class="com.zz.pojo.People">
<property name="name" value="李四"/>
</bean>
3.使用注解开发
在spring4后要保证aop包存在
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-irncuFDy-1584778596018)(C:\Users\19353\AppData\Roaming\Typora\typora-user-images\image-20200321115516293.png)]
使用注解要加入注解支持
<?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>
1.bean
1.1 Component:组件
//等价于<bean id ="user" class="com.zz.pojo.User"/>
@Component
public class User {
public String name="张三";
}
在使用Component注解时要指定扫描的包
<!--指定要扫描的包,这个包下的注解就会生效--> <context:component-scan base-package="com.zz.pojo"/> <context:annotation-config/>
[email protected]:相当于<property name=“name” value=“张三”
@Component
public class User {
//相当于<property name="name" value="张三"
@Value("张三")
public String name;
}
1.3衍生的注解
@Component有几个衍生的注解,我们在web开发中,分三层架构
- dao: @Repository
- service : @Service
- controller : @Controller
这四个注解作用相同,都是将某个类注册到Spring中,装配Bean
2.Scope:指定模式
@Scope("singleton")
<!--单例模式-->
<bean scope="singleton"/>
<!--原型模式-->
<!-- <bean scope="prototype"/>-->
4.javaConfig:
不需要xml了,直接由javaConfig管理
@Controller
public class User {
@Value("阿三")
private String name;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
//Configuration代表这是一个配置类,beans.xml
//这个也会spring容器托管,注册到容器中,因为它本来就是一个@Component
@Configuration
public class MyConfig {
@Bean
public User getUser(){
return new User();
}
}
public class MyTest {
public static void main(String[] args){
ApplicationContext context = new AnnotationConfigApplicationContext(MyConfig.class);
User getUser = (User)context.getBean("getUser");
System.out.println(getUser.getName());
}
下一篇: Spring注解开发