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;}
原创粉丝点击
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 手机qq群自动发广告怎么办 济宁学院考研和上课冲突怎么办 考研但是大四上课多怎么办 试管促排卵泡少怎么办 京东退货卖家拒收怎么办 京东退货被卖家拒收怎么办 期货平台跑路了怎么办 浮云牧场没房了怎么办 融资股票停牌了怎么办 买入的股票停牌怎么办 淘宝抢到便宜货老板不发货怎么办 微信代购买到假货了怎么办 微信代购收到假货怎么办 苹果商店下载很慢怎么办 谷歌商店下载东西慢怎么办 买家说少发货了怎么办 人肉代购被海关扣了怎么办 韩国代购被海关扣了怎么办 爱奇艺开通自动续费忘了账号怎么办 小米手机云储存空间不足怎么办 路由器被黑了打不开网页怎么办 致人轻伤跑了怎么办 轻伤对方要30万怎么办 老公用老婆的钱怎么办 想注册个公司要怎么办 域名续费不知道找谁怎么办 代收快递弄丢了怎么办 货到付款的快递人不在怎么办 快递送货上门人不在怎么办 ems快递签收人不在怎么办 快递被别人取了怎么办 怎么办快递宗和收发点 快递电话写错了怎么办 网上买沙发想退货怎么办 买的电脑想退货怎么办 买了衣服想退货怎么办 天猫买药审核通过后不要了怎么办 京东维修无发票怎么办 京东维修没有发票怎么办 苹果6s外音没了怎么办 苹果6splus开不了机怎么办