文章目录
使用注解配置开发使用JavaConfig实现配置
使用注解配置开发
使用注解开发必须导入Context的包和约束, 还有AOP的包, 并且开启自动配置和包的扫描(重要)
<?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"
xmlns
:aop
="http://www.springframework.org/schema/aop"
xsi
:schemaLocation
="http
://www
.springframework
.org
/schema
/beans
http
://www
.springframework
.org
/schema
/beans
/spring
-beans
.xsd
http
://www
.springframework
.org
/schema
/context
http
://www
.springframework
.org
/schema
/context
/spring
-context
.xsd
http
://www
.springframework
.org
/schema
/aop
http
://www
.springframework
.org
/schema
/aop
/spring
-aop
.xsd"
>
<!--扫描指定包下面的注解
-->
<context
:component
-scan base
-package="listen"/>
<!--开启自动配置
-->
<context
:annotation
-config
/>
<!-- 使用spring创建对象
, 在spring中这些都称为bean
-->
</beans
>
创建bean: @Component : 组件放到类上使用, 说明这个类交给spring管理, 也就是spring会自动将这个类创建成一个bean属性注入: @Value(“XXX”) : 组件进行属性注入, 相当于
@Component
@Scope("singleton")
public class User {
@Value("Bike")
public String name
;
}
衍生注解: @Component有几个衍生注解, 在我们web开发的时候, 会按照mvc三层去分, 所以他就衍生出一下三个注解
dao层 @Repository
service层 @Service
controller层 @Controller 其实这三个也是组件,功能是和@Component一样的, 主要注解了就说明这个类是托管给spring管理, 只是为了阅读性, 标识出来方便判断, 但是一定要确保在被扫描的包下
@Repository
public class UserDao {
}
@Service
public class USerService {
}
@Controller
public class UserController {
}
自动装配置: @AutoWired和@Resource我在前一篇文章主要就是讲了自动装配作用域: @Scope(“singleton”) 就会表示这个类使用的是单例模式 换成prototype就是原型模式了
@Component
@Scope("singleton")
public class User {
@Value("Bike")
public String name
;
}
总而言之: xml更加万能, 适用于任何场合 注解不是自己的类使用不了, 只能修饰自己的类, 对于被修饰的类, 不能引入其他类的bean对象, 维护相对复杂 所以 xml:负责管理bean 注解:负责属性的注入 我们在使用的过程中, 只需要注意必须要让注解生效, 也就是开启注解支持, 还有就是注解必须被扫描到
<!--扫描指定包下面的注解
-->
<context
:component
-scan base
-package="listen"/>
<!--开启自动配置
-->
<context
:annotation
-config
/>
实现在spring中去获取bean对象
public class Test {
public static void main(String
[] args
) {
ApplicationContext context
= new ClassPathXmlApplicationContext("applicationContext.xml");
User user
= context
.getBean("user", User
.class);
System
.out
.println(user
.name
);
}
}
使用JavaConfig实现配置
这是一个纯使用java的方式配置spring,可以完全不使用spring xml文件的配置, 全权由java复制, spring4之后的一个核心功能@Configuration这个注解标识的类也会被spring托管, 只是他这个注解修饰的类表示这是一个配置类, 是java代码的一个配置类, 而不是一个xml文件的配置类, 使用这个注解不扫描包也可以使用, 当然也可以在其下面加上@ComponentScan("…")去指定;路径去扫描@Bean在@Configuration这个注解标识的类里面使用, 表示注册一个Bean, 相当于我们xml文件中<bean标签完成的一个bean对象, 其中id就是方法的名称, 方法返回值类型就是class
@Configuration
@ComponentScan("bike.model")
public class MyConfig {
@Bean
public User
getUser() {
return new User();
}
}
如果有多个类被@Configuration修饰, 也可以导成一个使用 @Import(MyConfig.class)
@Configuration
@Import(MyConfig
.class)
public class MyConfig2 {
@Bean
public User
User() {
return new User();
}
}
如果完全使用配置类去完成配置工作, 我们就必须使用annotationConfig 上下文去获取配置类中的class对象的加载
public class Test1 {
public static void main(String
[] args
) {
ApplicationContext context
= new AnnotationConfigApplicationContext(MyConfig2
.class);
User user
= context
.getBean("getUser", User
.class);
System
.out
.println(user
.getName());
System
.out
.println("=================");
User user1
= context
.getBean("User", User
.class);
System
.out
.println(user1
.getName());
}
}
使用JavaConfig这种方式在spring Boot中使用很常见