在Spring4之后,要使用注解开发,必须要保证aop的包导入
使用注解需要导入context约束,增加注解的支持!
<?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
2.属性如何注入
package com.xu.dao;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
//@Component 组件
// 等价于<bean id="user" class ="com.xu.pojo.User"/>
@Component
public class User {
//相当于<property name="name" value="许冬华”/>
public String name ;
@Value("许冬华")
public void setName(String name) {
this.name = name;
}
}
3.衍生的注解
@Component有几个衍生注解,我们在web开发中,会按照mvc三层架构分层!
1.dao【@Repository】
2.service【@Service】
3.controller【@Controller】
这四个注解功能都是一样的,都是代表将某个类注册到spring中,装配Bean
4,自动装配
5.作用域
package com.xu.pojo; import org.springframework.beans.factory.annotation.Value; import org.springframework.context.annotation.Scope; import org.springframework.stereotype.Component; //@Component 组件 // 等价于<bean id="user" class ="com.xu.pojo.User"/> @Component @Scope("prototype")原型模式 @Scope("singleton")单例模式 public class User { //相当于<property name="name" value="许冬华”/> public String name ; @Value("许冬华") public void setName(String name) { this.name = name; } }
6.小结
xml与注解:
xml更加万能,适用任何场合!维护简单方便
注解不是自己类使用不了,维护相对复杂!
xml与注解最佳实践:
xml用来管理bean;
注解只负责完成属性的注入;
我们在使用的过程中,只需要注意一个问题;必须让注解生效,就需要开启注解的支持
<context:annotation-config/>开启注解 <context:component-scan base-package="com.xu"/>指定要扫描的包,这个包下的注解就会生效
使用Java的方式配置spring
我们现在要完全不使用Spring的xml配置了,全权交给Java来做!
javaConfig是spring的一个子项目,在spring4之后,它成为了一个核心功能!
实体类
package com.xu.pojo;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
@Component
public class User {
private String name;
public String getName() {
return name;
}
@Value("ddddd")
public void setName(String name) {
this.name = name;
}
@Override
public String toString() {
return "User{" +
"name='" + name + '\'' +
'}';
}
}
配置文件
package com.xu.config;
import com.xu.pojo.User;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;
//这个也会Spring容器托管,注册到容器中,因为他本身就是一个@Component
//@configuration代表这是一个配置类,就和我们之前看到的beans.xml
@Configuration
@ComponentScan("com.xu.pojo")
@Import(xuConfig2.class)
public class xuConfig {
@Bean
//注册一个bean,就相当于我们之前写的一个bean标签
//这个方法的名字,就相当于bean标签中的id属性
//这个方法的返回值,就相当于bean标签中的class属性
public User user(){
return new User();//就是返回要注入到bean的对象!
}
}
测试类!
import com.xu.config.xuConfig;
import com.xu.pojo.User;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class Mytest {
public static void main(String[] args) {
//如果完全使用了配置类方式去做,我们就只能通过AnnotationConfig 上下文获取容器,通过配置类的class对象加载!
AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(xuConfig.class);
User user = context.getBean("user", User.class);
System.out.println(user.getName());
}
}
这种纯Java的配置方式,在SpringBoot中随处可见!