在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的作用
首先我在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的作用
<beans>
<bean id="getUser" class="com.kk.pojo.User"/>
</beans>
- 注册一个bean,就相当于之前写的一个bean标签
- 这个方法的名字(getUser)就相当于bean标签中的id
- @Bean(“myThing”) 指定名称
- 这个方法的返回值(User)就相当于bean标签中的class属性
实际上是通过反射、代理来实现的
- @Bean 初始化和销毁方法指定
@Bean
注解支持指定任意初始化和销毁回调方法,非常类似于 Spring XML 在bean
元素上的init-method
和destroy-method
属性,如以下示例所示:
public class BeanOne {
public void init() {
// initialization logic
}
}
public class BeanTwo {
public void cleanup() {
// destruction logic
}
}
@Configuration
public class AppConfig {
@Bean(initMethod = "init")
public BeanOne beanOne() {
return new BeanOne();
}
@Bean(destroyMethod = "cleanup")
public BeanTwo beanTwo() {
return new BeanTwo();
}
}
@componentscan的作用
@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文件
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);
}
}
![图片[1]-Spring配置类的基本使用](http://8cltw.oss-cn-hongkong.aliyuncs.com/wp-content/uploads/2024/04/20240426225803616-1024x501.jpg)
![图片[2]-Spring配置类的基本使用](http://8cltw.oss-cn-hongkong.aliyuncs.com/wp-content/uploads/2024/04/20240426225836845-945x1024.jpg)
© 版权声明
文章版权归作者所有,未经允许请勿转载。
THE END
暂无评论内容