我们使用spring框架也会使用到配置文件,我们需要在src下创建一个关于spring的配置文件,
一般情况名称叫applicationContext.xml

基本约束:

<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN 2.0//EN""http://www.springframework.org/dtd/spring-beans-2.0.dtd">

IOC快速入门

  inversion of Controller 控制反转

原来由我们自己实例化的对象交给spring容器来实始化。这时对象的实始化的权利就会反转

创建一个类

public class Bean1 {public void show() {System.out.println("bean1......");}
}

在src下的applicationContext.xml中配置

<bean name="bean1" class="com.learn.bean.Bean1" scope="singleton"></bean>

在测试类中测试

  @Testpublic void test1() {ApplicationContext context = new ClassPathXmlApplicationContext("ApplicationContext.xml");UserServiceImpl us = (UserServiceImpl) context.getBean("UserService");us.sayHello();}

ApplicationContext它是BeanFactory的一个子接口,我们在使用时使用的是AppliCationContext的实现类ClassPathXmlApplicationContext
ApplicationContext它是扩展BeanFactory接口。BeanFactory它采取延迟加载的方案,只有真正在getBean时才会实例化Bean

可以通过getBean(配置文件中id名称)来获取指定的对象。

这就是一个基本的spring入门

Bean的实例化方式(这里介绍两种)

这种方式:bean类必须提供无参构造

配置

<bean name="bean1" class="com.learn.bean.Bean1" scope="singleton"></bean>

bean

public class Bean1 {public void show() {System.out.println("bean1......");}
}

测试

    //测试无参构造的实例化方式
    @Testpublic void test1() {ApplicationContext context = new ClassPathXmlApplicationContext("ApplicationContext.xml");Bean1 bean1 = (Bean1) context.getBean("bean1");bean1.show();}

静态工厂方法实例化

bean

public class Bean2 {public static Bean2 Bean2Factory() {return new Bean2();}public static  void show() {System.out.println("bean2");}
}

配置

         <!-- 静态工厂创建实例 --><bean name="bean2" class="com.learn.bean.Bean2" factory-method="Bean2Factory"></bean>            

需要注意的是,在工厂类中提供一个static返回bean对象

测试

  //测试静态工厂的实例化方式
    @Testpublic void test2() {ApplicationContext context = new ClassPathXmlApplicationContext("ApplicationContext.xml");Bean2 bean2 = (Bean2) context.getBean("bean2");bean2.show();}

DI

DI:dependency injection 依赖注入

在spring框架负责创建Bean对象时,动态将依赖对象注入到Bean组件。

第一种属性注入方式:
一个类

public class UserServiceImpl implements UserService {//声明一个依赖注入的属性//提供get/set方法private String INFO;public String getINFO() {return INFO;}public void setINFO(String iNFO) {INFO = iNFO;}@Overridepublic void sayHello() {System.out.println("不忘初心,"+INFO);}
}

属性注入

        <!-- setter注入属性 --><bean class="com.learn.domain.Person" name="person"><property name="name" value="张三"></property><property name="car" ref="car"></property></bean>

测试

  //测试构造器注入
    @Testpublic void test() {ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("ApplicationContext.xml");Car car = (Car) context.getBean("car");System.out.println(car);}

第二种属性注入方式:

        <!-- 构造器注入属性 --><bean name="car" class="com.learn.domain.Car"><constructor-arg type="String" value="宝马"/><constructor-arg type="Double" value="1000000"></constructor-arg></bean>

bean类

public class Car {private String name;private Double price;public Car(String name, Double price) {super();this.name = name;this.price = price;}public Car() {super();// TODO Auto-generated constructor stub
    }public String getName() {return name;}public void setName(String name) {this.name = name;}public Double getPrice() {return price;}public void setPrice(Double price) {this.price = price;}@Overridepublic String toString() {return "Car [name=" + name + ", price=" + price + "]";}
}

集合属性的注入:

public class CollectionDemo {private List list;private Set set;private Map map;private Properties prop;public CollectionDemo() {super();// TODO Auto-generated constructor stub
    }public List getList() {return list;}public void setList(List list) {this.list = list;}public Set getSet() {return set;}public void setSet(Set set) {this.set = set;}public Map getMap() {return map;}public void setMap(Map map) {this.map = map;}public Properties getProp() {return prop;}public void setProp(Properties prop) {this.prop = prop;}public CollectionDemo(List list, Set set, Map map, Properties prop) {super();this.list = list;this.set = set;this.map = map;this.prop = prop;}
}

配置注入属性

<!-- 集合注入 --><bean name="collection" class="com.learn.domain.CollectionDemo"><!-- list集合注入 --><property name="list"><list><value>李四</value><value>18</value><ref bean="car"/></list></property><!-- set属性注入 --><property name="set"><set><value>王五</value><value>20</value><ref bean="car"/></set></property><!-- map属性注入 --><property name="map"><map><entry key="username" value="tom"></entry><entry key="password" value="123"></entry></map></property><!-- properties属性注入 --><property name="prop"><props><prop key="username">123</prop><prop key="password">123</prop></props></property></bean>

bean的作用域:

  在bea声明时它有一个scope属性,它用于描述bean的作用域:

singleton:单例 代表在spring ioc容器中只有一个Bean实例 (默认的scope)
prototype多例 每一次从spring容器中获取时,都会返回一个新的实例
request 用在web开发中,将bean对象request.setAttribute()存储到request域中
session 用在web开发中,将bean对象session.setAttribute()存储到session域中
在开如常用的值是singleton与prototype

bean的声明周期

  

1. instantiate bean对象实例化
2. populate properties 封装属性
3. 如果Bean实现BeanNameAware执行setBeanName
4. 如果Bean实现BeanFactoryAwar或ApplicationContextAwar设置工厂setBeanFactory或上下文对象setApplicationContext
5. 如果存在类实现BeanPostProcessor(后处理Bean),执行postProcessBeforeInitialization
6. 如果Bean实现InitializingBean执行afterPropertiesSet
7. 调用自定义的init-method方法
8. 如果存在类实现BeanPostProcessor(处理Bean),执行postProcessAfterInitialization
9. 执行业务处理
10. 如果Bean实现DisposableBean执行destroy
11. 调用自定义的destroy-method

下面用代码来描述声明周期:

public class BeanLifeCycle implements BeanNameAware,ApplicationContextAware,InitializingBean,DisposableBean{private String name;public BeanLifeCycle() {System.out.println("第一步:实例化beanLifeCycle对象");}public String getName() {return name;}public void setName(String name) {System.out.println("第二步:name属性的注入");this.name = name;}@Overridepublic void setBeanName(String arg0) {System.out.println("第三步:得到bean的id或name属性的值"+name);}@Overridepublic void setApplicationContext(ApplicationContext arg0) throws BeansException {System.out.println("第四步:得到applicationContext对象");}@Overridepublic void afterPropertiesSet() throws Exception {System.out.println("第六步:调用属性注入完成后的方法");}public void myInit() {System.out.println("Init");System.out.println("第七步:自定义init方法");}public void add() {System.out.println("第九步:执行bean自己的功能");}@Overridepublic void destroy() throws Exception {System.out.println("第十步:调用销毁的方法");}public void myDestroy() {System.out.println("myDestory");System.out.println("第十一步:自定义销毁方法");}
}

public class MyBeanPostProcess implements BeanPostProcessor{@Overridepublic Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {System.out.println("第八步:beanPostAfter的方法");return bean;}@Overridepublic Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {System.out.println("第五步:beanPostBefore的方法");System.out.println("处理的bean是:"+bean +"它的名称是:"+beanName);return bean;}}

<bean name="beanLifeCycle" class="com.learn.bean.BeanLifeCycle" init-method="myInit" destroy-method="myDestroy"><property name="name" value="beanLifeCycle"></property></bean><bean name="myBeanPostProcess" class="com.learn.bean.MyBeanPostProcess"></bean>

测试:

  @Testpublic void test2() {ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("ApplicationContext.xml");BeanLifeCycle blc = (BeanLifeCycle) context.getBean("beanLifeCycle");blc.add();context.close();}

对于bean的生命周期,我们需要关注的主要有两个方法:
1. 增强bean的功能可以使用后处理Bean, BeanPostProcessor
2. 如果需要初始化或销毁操作我们可以使用init-method destroy-method

注意:destroy-method只对scope=singleton有效果。

SpEl

spring expression language 是在spring3.0以后的版本提供
它类似于ognl或el表达式,它可以提供在程序运行时构造复杂表达式来完成对象属性存储及方法调用等。
Spel表达式的格式 #{表达式}

        <bean class="com.learn.domain.Person" name="person"><property name="name" value="张三"></property><property name="car" ref="car"></property></bean><!-- spEL --><bean name="person1" class="com.learn.domain.Person"><property name="name" value="猥琐逼"></property><property name="car" value="#{person.car}"></property></bean>

public class Person {private String name;private Car car;public Person() {super();}public String getName() {return name;}public void setName(String name) {this.name = name;}public Car getCar() {return car;}public void setCar(Car car) {this.car = car;}public Person(String name, Car car) {super();this.name = name;this.car = car;}@Overridepublic String toString() {return "Person [name=" + name + ", car=" + car + "]";}
}

Spring注解开发
在spring中使用注解,我们必须在applicationContext.xml文件中添加一个标签
<context:annotation-config/>作用是让spring中常用的一些注解生效。
要使用contex名称空间,必须在applicationContext.xml文件中引入

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation="
        http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsdhttp://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">

快速入门

@Component("customer")
public class Customer {@Value("张三")private String name;public Customer(String name) {super();this.name = name;}public Customer() {super();}public String getName() {return name;}public void setName(String name) {this.name = name;}@Overridepublic String toString() {return "Customer [name=" + name + "]";}}

@Testpublic void test2() {ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");Customer c = (Customer) context.getBean("customer");System.out.println(c);}

注意:运行之前需要在applicationContext中配置:

<context:component-scan base-package="com.learn"></context:component-scan>

在spring2.5后为@Component添加了三个衍生的注解
@Rpository 用于DAO层
@Service 用于service层
@Controller 用于表现层
对于我们的bean所处在的位置可以选择上述三个注解来应用,如果你的bean不明确位置,就可以使用@Component.

属性注入

    @Value("张三")private String name;

复杂属性注入

  

    @Autowiredprivate CustomerDaoImpl cdi;

@Value @Autowired它们可以修饰属性,也可以修饰setter方法,如果写在属性上,就不需要提供setter方法。
@Autowired它默认是根据类型进行注入。
如果与@Qualifier一起使用,就可以根据名称来进行注入。
我们也可以使用下面的方式来根据名称进行属性注入

其它注解
@Scope它以描述bean的作用域。

@PostConstruct
public void myInit(){}

@PostConstruct相当于:init-method="myInit"

@PreDestory
public void myDestory(){}

@PreDestory相当于:destroy-method=”myDestroy”
注意:对于销毁的方法它只对bean的scope=singleton有效。

Spring整合junit4测试

//整合
@RunWith(SpringJUnit4ClassRunner.class)
//声明加载的配置文件
@ContextConfiguration(locations = "classpath:applicationContext.xml")
public class Test2 {@Autowiredprivate CustomerServiceImpl csi;@Autowiredprivate CustomerDaoImpl cdi;@Testpublic void test1() {csi.add();}@Testpublic void test2() {cdi.add();}
}

转载于:https://www.cnblogs.com/learnjfm/p/7131152.html

spring IOC快速入门,属性注入,注解开发相关推荐

  1. Spring优点、Spring IOC 底层实现原理、Spring IOC 快速入门案例、何为 DI 依赖注入

    Spring 概述 Spring 是一个开源框架,Spring 为简化企业级应用开发而生,使用 Spring 可以使简单的 JavaBean 实现以前只有 EJB 才能实现的功能.Spring 是 J ...

  2. Spring IOC快速入门

    Ioc是什么,能够解决什么问题,它的原理是如何实现的呢? IOC inversion of Controller 控制反转. 在程序中所说的IOC其实简单说,就是原来由我们自己实例化的对象交给spri ...

  3. Spring之IOC快速入门(一)

    2019独角兽企业重金招聘Python工程师标准>>> 文档版本 开发工具 测试平台 工程名字 日期 作者 备注 V1.0 2016.06.21 lutianfei none 本文内 ...

  4. 【720开发】 spring boot 快速入门

    spring boot 快速入门 通过构建简单的REST应用,了解spring boot的开发基本流程,验证其简单.易用特性. 环境要求 Spring Boot 2.0.0.BUILD-SNAPSHO ...

  5. 2.3.2 spring属性注入-注解注入-半注解注入-后序

    代码: spring属性注入-注解注入-半注解后序.zip - 蓝奏云文件大小:15.2 K|https://www.lanzouw.com/iCjbVvpvxaf 上一个博客的半注解是,一个类在xm ...

  6. Spring框架快速入门

    目录 什么是Spring? 为什么学习Spring? Spring的快速入门 Spring的版本 IOC概述 什么是IOC? Spring的IoC入门 下载Spring的开发包 创建web项目,引入S ...

  7. 视频教程-Spring框架快速入门到精通-Java

    Spring框架快速入门到精通 十年项目开发经验,主要从事java相关的开发,熟悉各种mvc开发框架. 王振伟 ¥18.00 立即订阅 扫码下载「CSDN程序员学院APP」,1000+技术好课免费看 ...

  8. spring boot 快速入门

    文章来源:https://www.cnblogs.com/junyang/p/8151802.html spring boot入门 -- 介绍和第一个例子 "越来越多的企业选择使用sprin ...

  9. 视频教程-Spring boot快速入门-Java

    Spring boot快速入门 十年项目开发经验,主要从事java相关的开发,熟悉各种mvc开发框架. 王振伟 ¥12.00 立即订阅 扫码下载「CSDN程序员学院APP」,1000+技术好课免费看 ...

  10. HealthKit开发快速入门教程之HealthKit开发概述简介

    HealthKit开发快速入门教程之HealthKit开发概述简介 2014年6月2日召开的年度开发者大会上,苹果发布了一款新的移动应用平台,可以收集和分析用户的健康数据.该移动应用平台被命名为&qu ...

最新文章

  1. jenkins job构建后汇总结果到同一个文本文档中去
  2. go语言之行--基础部分
  3. PHP利用P3P实现跨域
  4. LeetCode 1737. 满足三条件之一需改变的最少字符数(计数)
  5. [算法题] Search in Rotated Sorted Array
  6. vuejs 和 element 搭建的一个后台管理界面
  7. 3.3_number_join_数字拼接问题
  8. inno setup相关 (二)
  9. plsql可执行别的用户对象,过程却不行(AUTHID CURRENT_USER)
  10. discuzcode函数
  11. xrd连续扫描和步进扫描_深度解析XRD
  12. 4G/5G多链路聚合路由器对不同运营商聚合适用的工作场景
  13. 如何查看Ubuntu版本
  14. 你如何理解软件测试?
  15. yaml UnicodeDecodeError: ‘gbk‘ codec can‘t decode byte 0xba in position 15: illegal multibyte sequen
  16. UI设计师都用什么工具?
  17. html页面太大了怎么调小,html – 如何在调整浏览器窗口大小时保持绝对定位的元素...
  18. 国企安逸?我笑了……
  19. 恕我直言,在座的各位根本写不好Java!
  20. BTE1650 FBL1N/FBL2N/FBL3N/FBL5N-增加客制化字段 客商及科目描述

热门文章

  1. ALEIYE 2.0发布 首创RET关键事件功能
  2. 优化 Go 中的 map 并发存取
  3. Description Resource Path Location Type The superclass javax.servlet.http.HttpServlet was not foun
  4. string-indexOf、substring、split
  5. php判断手机是安卓系统还是ios系统
  6. U-Boot在FL2440上移植(四)----支持网卡DM9000和烧写yaffs文件系统
  7. Ubuntu各个版本的介绍
  8. java action url,Java ViewHandler.getActionURL方法代码示例
  9. linux命令last格式,Linux last和lastlog命令:查看过去登陆的用户信息
  10. IDEA中添加Tomcat配置,Run/Debug Configurations中没有Tomcat选项