目录
一、@Configuration
SpringConfig配置类
package com.example;
import org.springframework.context.annotation.Configuration;
@Configuration
public class SpringConfig {
}
二、@ComponentScan
1. 说明
SpringConfig配置类
package com.example;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
@Configuration
@ComponentScan("com.example")
public class SpringConfig {
}
2. 测试方法
// 测试纯注解
@Test
public void t4(){
ApplicationContext ac = new AnnotationConfigApplicationContext(SpringConfig.class);
StudentService service = ac.getBean("studentService",StudentService.class);
System.out.println(service.findById(8));
}
3. 运行结果
三、@PropertySource
1. 说明
JdbcConfig配置类
package com.example;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
import java.sql.Connection;
import java.sql.DriverManager;
@Configuration
@PropertySource("classpath:db.properties")
public class JdbcConfig {
@Value("${jdbc.username}")
private String username;
@Value("${jdbc.password}")
private String password;
@Override
public String toString() {
return "JdbcConfig[ " +
"username='" + username + '\'' +
", password='" + password + '\'' +
" ]";
}
}
2. 测试方法
// 测试纯注解扫描jdbc
@Test
public void t5(){
ApplicationContext ac = new AnnotationConfigApplicationContext(JdbcConfig.class);
JdbcConfig config = (JdbcConfig) ac.getBean("jdbcConfig");
System.out.println(config);
}
3. 测试结果
四、@Bean
1. 说明
2. 添加驱动依赖
在pom.xml文件添加依赖
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.27</version>
</dependency>
3. 将Connection对象放入Spring容器
@Bean("connection")
public Connection getConnection(){
try{
Class.forName("com.mysql.cj.jdbc.Driver");
return DriverManager.getConnection("jdbc:mysql:///mysql","root","666666");
}catch (Exception e){
e.printStackTrace();
return null;
}
}
3. 测试
测试方法
// 测试注解bean
@Test
public void t6(){
ApplicationContext ac = new AnnotationConfigApplicationContext(SpringConfig.class);
Connection connection = (Connection) ac.getBean("connection");
System.out.println(connection);
}
运行结果
五、@Import
1. 说明
package com.example;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;
@Configuration
@ComponentScan("com.example")
@Import(JdbcConfig.class)
public class SpringConfig {
}
2. 测试方法
// 测试纯注解
@Test
public void t4(){
ApplicationContext ac = new AnnotationConfigApplicationContext(SpringConfig.class);
JdbcConfig jdbcConfig = ac.getBean("jdbcConfig",JdbcConfig.class);
System.out.println(jdbcConfig);
}
3. 运行结果
往期专栏&文章相关导读
1. Maven系列专栏文章
Maven系列专栏 | Maven工程开发 |
Maven聚合开发【实例详解---5555字】 |
2. Mybatis系列专栏文章
Mybatis系列专栏 | MyBatis入门配置 |
Mybatis入门案例【超详细】 | |
MyBatis配置文件 —— 相关标签详解 | |
Mybatis模糊查询——三种定义参数方法和聚合查询、主键回填 | |
Mybatis动态SQL查询 --(附实战案例--8888个字--88质量分) | |
Mybatis分页查询——四种传参方式 | |
Mybatis一级缓存和二级缓存(带测试方法) | |
Mybatis分解式查询 | |
Mybatis关联查询【附实战案例】 | |
MyBatis注解开发---实现增删查改和动态SQL | |
MyBatis注解开发---实现自定义映射关系和关联查询 |
3. Spring系列专栏文章
Spring系列专栏 | Spring IOC 入门简介【自定义容器实例】 |
IOC使用Spring实现附实例详解 | |
Spring IOC之对象的创建方式、策略及销毁时机和生命周期且获取方式 | |
Spring DI简介及依赖注入方式和依赖注入类型 | |
Spring IOC相关注解运用——上篇 | |
Spring IOC相关注解运用——下篇 | |
Spring AOP简介及相关案例 | |
注解、原生Spring、SchemaBased三种方式实现AOP【附详细案例】 | |
Spring事务简介及相关案例 | |
Spring 事务管理方案和事务管理器及事务控制的API | |
Spring 事务的相关配置、传播行为、隔离级别及注解配置声明式事务 |