Java配置类:JavaConfig
在spring配置中 applicationContext.xml
<?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
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd">
<!-- 指定要扫描的包-->
<context:component-scan base-package="com.kk"/>
<!-- 开启注解的支持-->
<context:annotation-config/>
</beans>
@Configuration的作用
@Configuration用于定义配置类,可替换xml配置文件,被注解的类内部包含有一个或多个被@Bean注解的方法,这些方法将会被AnnotationConfigApplicationContext或AnnotationConfigWebApplicationContext类进行扫描,并用于构建bean定义,初始化Spring容器。
首先我在com.kk.config目录下创建一个自己的配置类 MyConfig
仔细阅读注解
-
@Configuration类似于applicationContext.xml中的beans
-
@Configuration 这个也会被Spring容器托管 注册到容器中 因为它本来就是一个@Component
-
@Configuration 代表这是一个配置类,就和之前的beans.xml一样
/**
* @Configuration类似于applicationContext.xml中的beans
* @Configuration 这个也会被Spring容器托管 注册到容器中 因为它本来就是一个@Component
* @Configuration 代表这是一个配置类,就和之前的beans.xml一样
*/
@Configuration
@ComponentScan("com.kk.pojo")
@Import(MyConfig_2.class)
public class MyConfig {
/**
* 注册一个bean,就相当于之前写的一个bean标签
* 这个方法的名字(getUser)就相当于bean标签中的id
*这个方法的返回值(User)就相当于bean标签中的class属性
*/
@Bean
public User getUser(){
return new User();//就是返回要注入到bean的对象
}
}
@Bean的作用
将这个类或者方法注册到spring IoC容器中,这个配置就等同于之前在xml里的配置
<beans>
<bean id="getUser" class="com.kk.pojo.User"/>
</beans>
-
注册一个bean,就相当于之前写的一个bean标签
-
这个方法的名字(getUser)就相当于bean标签中的id
-
这个方法的返回值(User)就相当于bean标签中的class属性
实际上是通过反射、代理来实现的
@componentscan的作用
@ComponentScan告诉Spring 哪个packages 用注解标识的类 ,会被spring自动扫描并且装入bean容器。
例如,如果你有个类用@Controller注解标识了,那么,如果不加上@ComponentScan,自动扫描该controller,那么该Controller就不会被spring扫描到,更不会装入spring容器中,因此你配置的这个Controller也没有意义。
@Import的作用
传入其他自定义配置类
@Configuration
public class MyConfig_2 {
}
测试:如果完全使用了配置类去做,就只能通过AnnotationConfig 上下文来获取容器,通过配置类的class对象加载
import com.kk.config.MyConfig;
import com.kk.pojo.User;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
public class Test {
public static void main(String[] args) {
//如果完全使用了配置类去做,就只能通过AnnotationConfig 上下文来获取容器,通过配置类的class对象加载
ApplicationContext context = new AnnotationConfigApplicationContext(MyConfig.class);
User user = context.getBean("user", User.class);
System.out.println(user.getName());
}
}
@PropertySource(“db.properties”)
作用:用于在Java配置文件中读取resources目录下的db.properties文件
db.properties
driver=com.mysql.cj.jdbc.Driver
url=jdbc:mysql://localhost:3306/mybatis?useSSL=true&useUnicode=true&characterEncoding=utf-8
user=root
password=123456
MyConfig.java
package com.kk.config;
import com.alibaba.druid.pool.DruidDataSource;
import com.kk.pojo.User;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.*;
@SuppressWarnings("all")
@Configuration
@PropertySource("db.properties")
public class MyConfig {
@Value("${drive}")
private String driverClassName;
@Value("${url}")
private String url;
@Value("${user}")
private String username;
@Value("${password}")
private String password;
@Bean
public DruidDataSource getDruidDataSource(){
DruidDataSource druidDataSource = new DruidDataSource();
druidDataSource.setDriverClassName(driverClassName);
druidDataSource.setUrl(url);
druidDataSource.setUsername(username);
druidDataSource.setPassword(password);
return druidDataSource;
}
}
测试:
package com.kk;
import com.kk.config.MyConfig;
import com.kk.pojo.User;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import javax.sql.DataSource;
/**
* @author : k
* @Date : 2022/4/9
* @Desc :
*/
public class TestMyConfigDataSource {
public static void main(String[] args) {
ApplicationContext context = new AnnotationConfigApplicationContext(MyConfig.class);
DataSource bean = context.getBean(DataSource.class);
System.out.println(bean);
}
}
已经读取到配置文件db.properties的数据