纯注解时第三方Bean
配置类:
import com.alibaba.druid.pool.DruidDataSource;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
@Configuration
@ComponentScan("aiqingit")
public class Config{
}
第三方bean(就是在配置类中加入东西,如下:):其中@Bean会将返回值作为配置文件bean,而测试类中更倾向于用类型调用,别忘了这里的返回值虽然是dS,但是是DataSource型,所以测试类就用DataSource.Class
放入getBean()中
import com.alibaba.druid.pool.DruidDataSource;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import javax.sql.DataSource;
@Configuration
public class Config{
@Bean
public DataSource jekt(){
DruidDataSource dS=new DruidDataSource();
dS.setDriverClassName("Driver");
dS.setUrl("jdbc:mysql://localhost:3306/parent");
dS.setUsername("root");
dS.setPassword("123456");
return dS;
}
}
测试类:
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import javax.sql.DataSource;
public class TestDay04 {
public static void main(String[] args) {
AnnotationConfigApplicationContext annotationConfigApplicationContext=new AnnotationConfigApplicationContext(Config.class);
DataSource dataSource =(DataSource) annotationConfigApplicationContext.getBean(DataSource.class);
System.out.println(dataSource);
}
}
结果运行成功:
{
CreateTime:"2023-03-02 11:23:08",
ActiveCount:0,
PoolingCount:0,
CreateCount:0,
DestroyCount:0,
CloseCount:0,
ConnectCount:0,
Connections:[
]
}
Process finished with exit code 0
调整一下,因为每次都需要添加新的bean进入配置类,没必要,所以就直接把新建的bean拎出来放在一个单独的类中,并且添加注解@Configuration,让注解类可以扫描到它的存在
Oversea类:
import com.alibaba.druid.pool.DruidDataSource;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import javax.sql.DataSource;
@Configuration
public class oversea {
@Bean
public DataSource jekt(){
DruidDataSource dS=new DruidDataSource();
dS.setDriverClassName("Driver");
dS.setUrl("jdbc:mysql://localhost:3306/parent");
dS.setUsername("root");
dS.setPassword("123456");
return dS;
}
}
或者Oversea类上不添加@Configuration也可以,那就在配置类的类上方添加@Import(),以类名.Class形式填入进行扫描(
@Import(Oversea.class)
)
测试类照旧,运行成功,与上面的结果类似
------------------------------------------------------------------------------------------------------------------------------------------------------------------------
在第三方Bean里添加简单与引用类型
简单类型很简单:在oversea类中添加属性,且在其上加@Value()注入想要的值就好:
@Value("王自如")
private String name;
@Value("董明珠")
private String name1;
public DataSource jekt(makabaka maka){
dataSource.setDriverClassName("name");
}
但是引用类型就不一样了,如下,直接在jekt()
方法中添加引用类型的参数 (这里的@Bean会自动根据参数的类型进行注入,另外别忘了这个参数的所属的类要添加注解@Component或者衍生类,用来在配置类扫描时注册进去成为bean)
用来存放第三方Bean的类(这里的类没有加入@Configuration,因为,配置类中将用@Import来扫描指定的类):
package aiqingit;
import com.alibaba.druid.pool.DruidDataSource;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import javax.sql.DataSource;
public class Oversea {
@Bean
public DataSource jekt(makabaka maka){
System.out.println(maka.prin()+"羯鼓👇");
DruidDataSource dataSource=new DruidDataSource();
dataSource.setDriverClassName("Driver");
dataSource.setUrl("jdbc:mysql://localhost:3306/parent");
dataSource.setUsername("root");
dataSource.setPassword("123456");
return dataSource;
}
}
makabaka类:
package aiqingit;
import org.springframework.stereotype.Repository;
@Repository("makabaka")
public class makabaka{
public void show(){
System.out.println("玛卡巴卡,一股辟谷");
}
public String prin(){
return "玛卡巴卡";
}
}
配置类:用注解@Import来添加需要扫描的类
import aiqingit.*;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;
@Configuration
@Import({Oversea.class,makabaka.class})
public class Config{
}
测试类:
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import javax.sql.DataSource;
public class TestDay04 {
public static void main(String[] args) {
AnnotationConfigApplicationContext annotationConfigApplicationContext=new AnnotationConfigApplicationContext(Config.class);
DataSource dataSource =(DataSource) annotationConfigApplicationContext.getBean(DataSource.class);
System.out.println(dataSource);
}
}
结果:由此可知引用类型成功运行(运用了自动装配)
玛卡巴卡羯鼓👇
{
CreateTime:"2023-03-02 14:26:30",
ActiveCount:0,
PoolingCount:0,
CreateCount:0,
DestroyCount:0,
CloseCount:0,
ConnectCount:0,
Connections:[
]
}
Process finished with exit code 0