编程杂谈

新博客:https://blog.bigdataboy.cn/article/448.html管理第三方Bean第三方Bean没有办法写给它注解,所以就只能添加,Spring的处理是在配置类中写一个函数,该函数使用@Beam注解第三方Bean配置类第三方Bean的配置类,建议单独文件编写,这样不冗余,全部写一个配置类,会变得一大坨importcom.alibaba.druid.pool.DruidDataSource;importorg.springframework.context.annotation.Bean;importorg.springframework.context.annotation.Configuration;importjavax.sql.DataSource;@ConfigurationpublicclassJdbcConfig{@Bean//表示返回一个Bean,也可以指定id->@Bean("dataSource")publicDataSourcedataSource(){//参数先空着DruidDataSourcedataSource=newDruidDataSource();dataSource.setDriverClassName("");dataSource.setUrl("");dataSource.setUsername("");dataSource.setPassword("");returndataSource;}}在主配置类导入第三方配置类@Configuration@ComponentScan({"cn.bigdataboy.dao"})@Import({JdbcConfig.class})//导入publicclassSpringConfig{}获取第三方Bean第三方Bean的注入资源上面的例子,JDBC的链接还空着,所以我们需要为其注入参数注入简单类型注入方式与注入自己的Bean一样,使用成员属性添加@Value()注解importcn.bigdataboy.dao.NameDao;importcom.alibaba.druid.pool.DruidDataSource;importorg.springframework.beans.factory.annotation.Value;importorg.springframework.context.annotation.Bean;importorg.springframework.context.annotation.Configuration;importjavax.sql.DataSource;@ConfigurationpublicclassJdbcConfig{@Value("${jdbc.driver}")publicStringdriverClassName;@Value("${jdbc.url}")publicStringurl;@Value("${jdbc.username}")publicStringusername;@Value("${jdbc.password}")publicStringpassword;@Bean//表示返回一个Bean,也可以指定id->@Bean("dataSource")publicDataSourcedataSource(){System.out.println(this.driverClassName);System.out.println(this.url);DruidDataSourcedataSource=newDruidDataSource();dataSource.setDriverClassName(this.driverClassName);dataSource.setUrl(this.url);dataSource.setUsername(this.username);dataSource.setPassword(this.password);returndataSource;}}注入引入类型这里注入引入类型,有点不一样,直接添加成方法参数就好,因为Spring容器中已经存在相应的Bean,那么它也就能自己找到,因此也容易想到它是使用类型方式找的importcn.bigdataboy.dao.NameDao;importcom.alibaba.druid.pool.DruidDataSource;importorg.springframework.context.annotation.Bean;importorg.springframework.context.annotation.Configuration;importjavax.sql.DataSource;@ConfigurationpublicclassJdbcConfig{@Bean//表示返回一个Bean,也可以指定id->@Bean("dataSource")publicDataSourcedataSource(NameDaonameDao){//添加成参数System.out.println(nameDao);//打印一下,表示注入成功DruidDataSourcedataSource=newDruidDataSource();//相应配置returndataSource;}}案例代码:https://pan.bigdataboy.cn/s/NO5sp

编程杂谈

新博客:https://blog.bigdataboy.cn/article/444.html注解开发配置文件的作用降低,只需要完成扫描器的定义就行,类上添加一个注解,就表示该类是容器管理的Bean类添加注解importcn.bigdataboy.service.NameService;importorg.springframework.stereotype.Component;@Component("nameService")//参数就是idpublicclassNameServiceImplimplementsNameService{publicvoidsave(){System.out.println("servicesave...");}}类注解的官方推荐使用importorg.springframework.stereotype.Controller;importorg.springframework.stereotype.Repository;importorg.springframework.stereotype.Service;importorg.springframework.stereotype.Component;@Controller//表现层@Service//业务层@Repository//数据层@Component("nameService")//其他层配置添加扫描器<?xmlversion="1.0"encoding="UTF-8"?><beansxmlns="http://www.springframework.org/schema/beans"xmlns:context="http://www.springframework.org/schema/context"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans.xsdhttp://www.springframework.org/schema/contexthttp://www.springframework.org/schema/context/spring-context.xsd"><!--定义扫描器上面的命名空间要完整,idea自动的生成的不完整--><context:component-scanbase-package="cn.bigdataboy"/></beans>纯注解开发不再需要配置文件,所以也很容易想到,配置文件转变成配置类,同时也就能想到,在初始化容器时,传入的也就不是配置文件,而是配置类了配置类格式importorg.springframework.context.annotation.ComponentScan;importorg.springframework.context.annotation.Configuration;@Configuration//表示配置类@ComponentScan({"cn.bigdataboy.dao","cn.bigdataboy.service"})//扫描器定义多个路径,可以通配,建议准确一点,毕竟有些类不建议Bean化publicclassSpringConfig{}使用importcn.bigdataboy.config.SpringConfig;importcn.bigdataboy.dao.NameDao;importcn.bigdataboy.service.NameService;importorg.springframework.context.annotation.AnnotationConfigApplicationContext;publicclassAppForAnnotation{publicstaticvoidmain(String[]args){//加载配置类AnnotationConfigApplicationContextcontext=newAnnotationConfigApplicationContext(SpringConfig.class);//从容器获取对象NameServicenameService=(NameService)context.getBean("nameService");NameDaonameDao=context.getBean("nameDao",NameDao.class);System.out.println(nameService);System.out.println(nameDao);}}案例代码:https://pan.bigdataboy.cn/s/y4jie