Spring 基于注解的IOC配置
来源:互联网 发布:剑侠情缘网络单机版 编辑:程序博客网 时间:2024/05/17 23:44
Spring 基于注解的IOC配置
1、准备工作
必备jar包:spring-aop-4.2.4.RELEASE.jar
基础包:spring-core、spring-beans、spring-context、spring-expression、log4j、commons-logging
在src下创建xml文件:
添加xml文档约束:
<!-- 导入schema 约束的位置在: ..\spring-framework-4.2.4.RELEASE\docs\spring-framework-reference\html\xsd-configuration.html 文件中。注意:要导入schema约束--><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 "></beans>
在xml文件中开启spring对注解IOC的支持:
<!-- 告知spring框架在,读取配置文件,创建容器时,扫描注解,依据注解创建对象,并存入容器中 --> <context:component-scan base-package=""></context:component-scan>
其中base-package的值为,要扫描的包名。
2、常用注解
2.1、勇于创建对象的,相当于:<bean id="" class=""></bean>
@Component
作用:
把资源让spring来管理。相当于在xml中配置一个bean。
属性:
Value:指定bean的id。默认值为当前类名(首字母小写)。
@Controller @Service @Repository
作用及属性与@Component相同,只不过是提供了更加明确的语义化
@Controller:一般用于表现层的注解。
@Service:一般用于业务层的注解。
@Repository:一般用于持久层的注解。
2.2 用于注入数据的,相当于:
<property name="" ref=""/>和<property name="" value=""/>
@AutoWired
作用:
自动按照类型注入。当使用注解注入属性时,不会调用set方法(set方法可以省略)。他只能注入其他bean类型。当有多个类型匹配时,使用要注入的对象变量名称作为bean的id,在spring容器查找,找到了可以注入成功,找不到就报错。
@Qualifier
作用:
在自动按照类型注入的基础之上,在按照Bean的id注入。他在给字段注入是不能独立使用,必须和@Autowire一起使用;但是方法参数注入时可以独立使用。
属性:
Value:指定bean的id
@Resource
作用:
直接按照Bean的id注入。它也只能注入其他bean类型。
属性:
Name:指定bean的id.
@value
作用:
注入基本数据类型和String类型数据的
属性:
value:用于指定值
2.3 用于改变作用范围的,相当于:<bean id="" class="" scope=""/>
@Scope
作用:
指定bean的作用范围。
属性:
Value:指定范围的值:singleton、prototype、request、session、globalsession
2.4 和生命周期相关的,相当于:
<bean id="" class="" init-method="" destroy-method="" />
@PostConstruct
作用:
用于指定初始化方法。
@PreDestory
作用:
用于指定销毁方法。
代码示例:
业务层代码:
/*客户业务接口*/Public interface ICustomerService{ Void saveCustomer();}/*客户业务层实现类*/@Component(value="customerService")@Scope(value="singleton")Public class CustomerServiceImpl implements ICutomerService { @Resource(name="customerDao") Private ICustomerDao customerDao = null; @value("com.mysql.jdbc.Driver") Private String driver; Public void saveCustomer(){ System.out.println(driver); customerDao.saveCustomer(); }}持久层代码:/*客户持久层接口*/Public interface ICustomerDao { Void saveCustomer();]/*客户持久层实现类*/@Repository("customerDao")Public class CustomerDaoImpl { Public void saveCustomer(){ System.out.println("保存客户信息"); }}测试类代码:Public class Client { Public static void main(String[] args){ ApplicetionContext ac = new ClassPathXmlApplicationContext("xml文件名"); ICustomerService cs = (ICustomerService) ac.getBean("customerService"); cs.saveCustomer(); }}
关于Spring注解和XML的选择
注解的优势:
配置简单,维护方便。
Xml的优势:
修改时不需要修改源码,不涉及重新编译和部署
Spring管理Bean方式的比较:
spring的纯注解配置
将xml中配置的关键的配置:<context:component-scan base-package="">
也生产注解
4.1 新注解说明:
@Configuration
作用:
用于指定当前类是一个spring配置类,当创建容器是会从该类上加载。获取容器时需要使用AnnotationApplicationContext(有@Configuration注解的类.class)。
属性:
Value:用于指定配置类的字节码
代码示例:
/*用于初始化spring容器的配置类*/ @Cpnfiguration Public class SpringConfiguration{ }
@ComponentScan
作用:
用于指定springzai在初始化容器时要扫描的包。作用和在spring的xml配置文件中的:<context:component-scan base-package="com.itheima"/>
一样
属性:
basePackage:用于指定要扫描的包。和该注解中的value属性作用一样。
@Import
作用:
用于倒入其他配置文件,被引入文件可以不写@Configuration注解
属性:
Value[]:用于指定其他配置类的字节码
代码示例:@Configuration@ComponentScan(basePackages="com.it.xxx")@Import({Configuration_B.class})Public class Configuration_A {} @Configuration//@PropertySource("classpath:info.properties")Public class Configuration_B {}
@Bean
作用:
该注解只能写在方法上,表明使用此方法创建一个对象,并且放入spring容器。它就相当于我们之前在xml配置中介绍的factory-bean和factory-method。
属性:
Name:给当前@Bean注解方法创建的对象指定一个名称(即bean的id).
代码示例:@Bean(name="datasource")Public DateSource createDS()throws Exception {ComboPooledDataSource comboPooledDataSource = new ComboPooledDataSource();comboPooledDataSource.setUser("root");comboPooledDataSource.setPassword("1234");comboPooledDataSource.setDriverClass("com.mysql.jdbc.Driver");comboPooledDataSource.setJdbcUrl("jdbc:mysql:///spring_ioc");return comboPooledDataSource;}
- Spring 基于注解的IOC配置
- 基于注解的IOC配置
- 【Spring】IOC之基于注解的配置bean(上)
- Spring 基于注解的IOC配置&整合Junit
- Spring IOC 基于注解的使用
- Spring--IoC--基于注解的DI-@Scope
- Spring IoC 注解配置
- Spring IoC 注解配置
- (spring-第4回【IoC基础篇】)spring基于注解的配置
- Spring框架总结——第二部分(基于xml配置+注解的方式配置IOC)
- 通过注解方式配置Spring的IoC
- spring的配置及ioc注解总结
- Spring--IoC--基于注解的DI-@Component相关注解
- 【Java.Spring.Core】【IoC】基于注解的Bean装配(非XML配置)
- spring基于注解的配置
- Spring基于注解的配置
- spring基于注解的配置
- Spring(四)基于注解配置IOC容器&基于注解实现声明式事务
- Linux 内存,cpu,io等信息的查看
- OpenCV-形态学处理-morphologyEx
- 学习Javascript闭包(Closure)
- Echarts柱状折线图
- VideoView 播放sd卡目录下视频的路径问题
- Spring 基于注解的IOC配置
- 深度学习(六十八)darknet使用
- JavaSE阶段1_类
- python 进程 Ctrl+C退出
- <li></li>标签的属性/样式
- 局域网映射的几种方式
- 2017年一级消防工程师高薪就业
- 1030. Travel Plan (30)
- JavaSE阶段1_抽象类和接口