Spring03_基于注解的IOC配置
1、环境搭建
1)jar包拷贝
这里对比xml的配置方式,jar包多了一个aop的jar包
2)使用@Component注解配置管理的资源
注意:当使用注解方式进行属性注入的时候set方法不用写
3)创建spring的配置文件,开启对注解的支持
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="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/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"> <!-- 告知 spring 创建容器时要扫描的包 --> <context:component-scan base-package="com.itheima"></context:component-scan>
<!-- 配置 dbAssit --> <bean id="dbAssit" class="com.itheima.dbassit.DBAssit"> <property name="dataSource" ref="dataSource"></property> </bean> <!-- 配置数据源 --> <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"> <property name="driverClass" value="com.mysql.jdbc.Driver"></property> <property name="jdbcUrl" value="jdbc:mysql:///spring_day02"></property> <property name="user" value="root"></property> <property name="password" value="1234"></property> </bean> </beans>
2、常用注解介绍
1)用于创建对象的
a:@Component
作用:把资源交给spring管理,相当于配置了一个bean
属性:
value:指定bean的id。如果不指定value属性,默认id为当前类的类名,首字母小写
b:@Controller @Service @Repository
这三个注解都是Component的子类注解,属性完全一样
但是这三个拥有更明确的语义:
2)用于注入数据的
a:@Autowried
作用:自动按照类型注入。
当使用注解注入属性时,set方法可以省略,因为只能注入其他的bean类型。当有多个类型匹配的时候,使用要注入的变量的名称作为bean的id,在spring容器中查找,找到就可以注入成功。
b:@Qualifier
作用:在自动按照类型注入的基础上,再按照bean的id注入。它在给字段注入时不能独立使用,必须和Autowried配合使用。但是给方法注入参数时可以独立使用
属性:value。指定bean的id
c:@Resource
作用:直接按照bean的id注入,它也只能注入其他的bean类型
属性:name。指定bean的id
d:@Value
作用:注入基本数据类型和String数据类型
属性:value。用于指定值
3)用于改变作用范围
@Scope
作用:指定bean的作用范围
属性:value:指定范围的值(取值:singleton prototype request session globalsession)
4)和声明周期相关
a:@PConstruct
作用:用于指定初始化方法
b:@PreDestory
作用:用于指定销毁方法
3、关于spring注解和xml的选择
注解优势:
配置简单,维护方便(找到对应的类,就找到了对应的配置)
xml优势:
修改时不用修改源码。不涉及重新编译和部署
spring管理bean的方式比较
4、新注解说明
1)@Configuration
作用:用于指定当前类是一个spring的配置类,当创建容器时会从该类上加载注解。获取容器的时候需要使用AnnotationApplicationContext(有@Configuration 注解的类.class)。
属性:value。用于指定配置类的字节码
示例代码: /** * spring 的配置类,相当于 bean.xml 文件*/ @Configuration public class SpringConfiguration { }
2)@ComponentScan
作用:用于指定spring在初始化容器时要扫描的包。作用和xml中的<context:component 是一样的。
属性:basePackages:用于指定要扫描的包,和注解中的value属性作用一样
示例代码:
@Configuration @ComponentScan("com.itheima") public class SpringConfiguration { }
3)@Bean
作用:该注解只能作用在方法上,表明使用此方法创建一个对象,并放置到spring容器中
属性:name。给当前@Bean创建的对象指定一个名称。相当于bean的id
示例:
/** * 创建一个数据源,并存入 spring 容器中 * @return */ @Bean(name="dataSource") public DataSource createDataSource() { try { ComboPooledDataSource ds = new ComboPooledDataSource(); ds.setUser("root"); ds.setPassword("1234"); ds.setDriverClass("com.mysql.jdbc.Driver"); ds.setJdbcUrl("jdbc:mysql:///spring_day02"); return ds; } catch (Exception e) { throw new RuntimeException(e); } } /** * 创建一个 DBAssit,并且也存入 spring 容器中 * @param dataSource * @return */ @Bean(name="dbAssit") public DBAssit createDBAssit(DataSource dataSource) { return new DBAssit(dataSource); }
4)@PropertySource
作用:用于加载.properties文件中的配置。例如数据源连接信息的配置,写入到properties文件,然后使用这个注解指定配置文件位置
属性:value[ ]。用于指定properties文件位置。如果在类路径下需要加classpath:
示例:
public class JdbcConfig { @Value("${jdbc.driver}") private String driver; @Value("${jdbc.url}") private String url; @Value("${jdbc.username}") private String username; @Value("${jdbc.password}") private String password; /** * 创建一个数据源,并存入 spring 容器中 * @return */ @Bean(name="dataSource") public DataSource createDataSource() { try { ComboPooledDataSource ds = new ComboPooledDataSource(); ds.setDriverClass(driver); ds.setJdbcUrl(url); ds.setUser(username); ds.setPassword(password); return ds; } catch (Exception e) { throw new RuntimeException(e); } } }
5)@Import
作用:用于导入其他配置类。在引入其他配置类是可以不用在写@Configuration注解。写上也可以
属性:value[ ]。用于指定其他配置类字节码
示例:
@Configuration @ComponentScan(basePackages = "com.itheima.spring") @Import({ JdbcConfig.class}) public class SpringConfiguration { } @Configuration @PropertySource("classpath:jdbc.properties") public class JdbcConfig{ }
6)通过注解的方式获取容器
ApplicationContext ac = new AnnotationConfigApplicationContext(SpringConfiguration.class);
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· SQL Server 2025 AI相关能力初探
· 单线程的Redis速度为什么快?
· AI编程工具终极对决:字节Trae VS Cursor,谁才是开发者新宠?
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码