1. 概要

struts2:web
hibernate:持久化
spring:业务层.管理bean的,容器.List Map Set.体验spring:1.创建java项目.2.引入spring的类库.${spring解压目录}/dist/spring.jar${spring解压目录}/lib/jakarta-commons/commons-logging.jar2'.创建业务类./*** GreetingService*/public class GreetingService {private String greeting ;//get/setprivate String greeting2 ;//get/set/* byeService */private ByeService bs ;//get/setpublic void  sayGreeting(){bs.sayBye();}}/*** ByeService*/public class ByeService {private String bye ;public String getBye() {return bye;}public void setBye(String bye) {this.bye = bye;}public void sayBye(){System.out.println(bye);}}3.配置spring的配置文件.src/applicationContext.xmlschema:${spring解压目录}\docs\reference\html_single\index.html\3.2.1.1<?xml version="1.0"?><beans xmlns="http://www.springframework.org/schema/beans"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans-2.5.xsd"><!-- 欢迎服务 --><bean id="greetingService" class="cn.itcast.spring.service.GreetingService"><property name="greeting"><value>hello world</value></property><property name="greeting2" value="tom" /><property name="bs" ref="byeService" /></bean><!-- 欢送服务 --><bean id="byeService" class="cn.itcast.spring.service.ByeService"><property name="bye"><value>later</value></property></bean></beans>4.创建app.ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml");GreetingService gs = (GreetingService) ac.getBean("greetingService");gs.sayGreeting();gs.sayGreeting2();ByeService bs = (ByeService) ac.getBean("byeService");bs.sayBye();spring:ioc: inverse of control,反转控制.获得依赖对象的方式被反转了. (不要手动new bean对象)1.new.(spring负责对象实例化)2.组装对象的出发点是反的(相对于bean的依赖关系是反的).DI:dependency injection,依赖注入.  <==> iocaop: aspect oriented program,面向方面编程.oop:面向对象编程.soa:GreetingService gs = new ...();gs.setByeService(bs);ByeService bs = new ...();
gs.sayGreeting();BeanFactory:实例化bf时,不会实例化任何bean,只有getBean的时候,才实例化相关的bean.节省资源.
ApplicationContext:实例化ac时,实例化单例bean.
懒汉式:
public class A{private A(){}public static A instance = null ;public static A getInstance(){if(instance == null){instance = new A();}return instance ;}
}饿汉式:public static A instance = new A();spring单利bean,对应的bean的叫做prototype(原型bean).ac.getBean("bs"); == new ByeService();生命周期:1.new2.set DI3.BeanNameAware:bean名关注4.BeanFactoryAware:bean工厂关注5.BeanPostProcessor.beforeInitialization();5'.调用InitializingBean.afterPropertySet()方法.6.init-method7.BeanPostProcessor.afterInitialization();spring()框架. 代码的入侵.
1.高侵入性
2.低侵入
3.非侵入行jee:jsp servlet jpa jta ejb(enterprise javabean),jndi
pojo:plain old java object.BeanDefinition:bean的定义,spring对bean的信息的封装.
Root bean: class [cn.itcast.spring.service.GreetingService]; scope=singleton; abstract=false; lazyInit=false;
autowireMode=0; dependencyCheck=0; autowireCandidate=true; primary=false; factoryBeanName=null;
factoryMethodName=null; initMethodName=ini; destroyMethodName=release; defined in class path resource
[applicationContext.xml]Class clazz = Class.forName("cn.itcast.spring.service.GreetingService");
clazz.newInstance();scope:prototype singleton request session global_session<struts><package namepace extends="">在ide下注册spring中的schema文件.${spring解压目录}\dist\resources\*.xsd.List Set Map:
Properies:
.properties<hibernate-mapping><class name table lazy><id ><generator class="" /></id><property name="age" column access="field"></class>spring通过构造函数参数注入依赖强化依赖关系.自动装配:1.byName:按照名称自动装配,寻找和bean的属性名相一致的bean的id.(bean必须有空的构造,通过set方法注入)2.byType:按照属性的类型来自动装配,如果找到多个,抛异常.(bean必须有空的构造,通过set方法注入)3.constructor:按照构造函数参数的类型自动装配,如果找不到或者找多个,都抛异常.4.autodetact:自动检测,在(2)和(3)之间选择一个 5.no.6.default,跟<beans default-autowire属性>保持一致.分散配置:把需要在上下文硬编码的属性拿到外部的属性文件中定义,让上下文从外部文件提取值.自定义编辑器:将字符串转换成相应的对象,

2. Spring中bean的生命周期, 及其工作流程

bean被载入到容器中时,他的生命周期就开始了。bean工厂在一个bean可以使用前完成很多工作:

1).容器寻找bean的定义信息并实例化。

2).使用依赖注入,spring按bean定义信息配置bean的所有属性。

3).若bean实现了BeanNameAware接口,工厂调用Bean的setBeanName()方法传递bean的ID。

4).若bean实现了BeanFactoryAware接口,工厂调用setBeanFactory()方法传入工厂自身。

5).若BeanPostProcessor和bean关联,则它们的postProcessBeforeInitialization()方法被调用。

6).若bean指定了ini-method方法、,它将被调用。

7).最后,若有BeanPostProcessor和bean关联,则它们的postProcessAfterInitialization()方法被调用、。

示例代码一: BeanPostProcessor 注册bean后处理器,能给所有bean做初始和收尾工作,等效于在xml文件中配置init-method="ini"  destroy-method="release"。

如果bean构造时 需要调用有参的构造函数,可以在constructor-arg 中配置。

ByeService.java , javabean

public class ByeService {private String bye ;public String getBye() {return bye;}public void setBye(String bye) {this.bye = bye;}public void sayBye(){System.out.println(bye);}
}

GreetingService.java, javabean

public class GreetingService implements BeanNameAware,BeanFactoryAware{private String greeting ;private String greeting2 ;/* byeService */private ByeService bs ;public GreetingService(ByeService bs){this.bs = bs ;System.out.println("Constructor: new GreetingService()");}public GreetingService(String str1,String str2){System.out.println("1111");}public GreetingService(String str1,Integer str2){System.out.println("2222");}public GreetingService(String str1,int str2){System.out.println("3333");}public GreetingService(int str1,String str2){System.out.println("4444");}/*** beannameAware,注入bean的id*/public void setBeanName(String name) {System.out.println("BeanNameAware:setBeanName() : " + name);}/*** bean工厂关注*/public void setBeanFactory(BeanFactory beanFactory) throws BeansException {System.out.println("BeanFactoryAware:setBeanFactory() : " + beanFactory);}public ByeService getBs() {return bs;}public void setBs(ByeService bs) {this.bs = bs;System.out.println("DI:setBs() : " + bs);}public String getGreeting() {return greeting;}public void setGreeting(String greeting) {this.greeting = greeting;System.out.println("DI:setGreeting() : " + greeting);}public void sayGreeting(){bs.sayBye();}public void sayGreeting2(){System.out.println(greeting2);}public String getGreeting2() {return greeting2;}public void setGreeting2(String greeting2) {this.greeting2 = greeting2;System.out.println("DI:setGreeting2() : " + greeting2);}/*** 定制初始化方法*/public void ini(){System.out.println("ini");}/*** 定制销毁方法*/public void release(){System.out.println("release");}
}

MyBeanPostProcessor.java, bean的后处理器

public class MyBeanPostProcessor implements BeanPostProcessor {/*** 后处理*/public Object postProcessAfterInitialization(Object bean, String beanName)throws BeansException {System.out.println("BeanPostProcessor:after:" + beanName);return bean;}/*** 之前处理*/public Object postProcessBeforeInitialization(Object bean, String beanName)throws BeansException {System.out.println("BeanPostProcessor:before:"+beanName);return bean;}
}

applicationContext.xml 配置文件

<?xml version="1.0"?>
<beans xmlns="http://www.springframework.org/schema/beans"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans-2.5.xsd"><!-- 欢迎服务 --><bean id="greetingService" class="cn.itcast.spring.service.GreetingService"scope="singleton"init-method="ini"destroy-method="release"><constructor-arg index="1" type="java.lang.String" value="12" /><constructor-arg index="0" type="int" value="24" /><property name="greeting"><value>hello world</value></property><property name="greeting2" value="tom" /><property name="bs" ref="byeService" /></bean><!-- 欢送服务 --><bean id="byeService" class="cn.itcast.spring.service.ByeService"><property name="bye"><value>later</value></property></bean><!-- 注册bean后处理器 --><bean id="myBeanPostProcessor" class="cn.itcast.spring.beanprocessor.MyBeanPostProcessor" />
</beans>

App.java

public class App {public static void main(String[] args) {ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml");//src下 不需要加全路径GreetingService gs = (GreetingService) ac.getBean("greetingService");gs.sayGreeting();gs.sayGreeting2();ByeService bs = (ByeService) ac.getBean("byeService");bs.sayBye();((ClassPathXmlApplicationContext)ac).destroy();}
}

3.  集合装配bean

单例和原型模式: prototype、singleton、request session、global-session ,spring中的bean缺省情况下是单例模式。始终返回一个实例。若想返回不同的实例的话需要定义成原型模式。bean的singleton属性告诉上下文该bean是否为单例的。缺省为true。若为false的话,为原型bean。

<bean id="foo" class="...Foo" singleton="false"/>

<!– spring2.5 -->

<bean scope="prototype|single|..">

 
自动装配:

<bean id="foo" class="...Foo" autowire="autowire type">

有四种自动装配类型:

1.byName:寻找和属性名相同的bean,若找不到,则装不上。

2.byType:寻找和属性类型相同的bean,找不到,装不上,找到多个抛异常。

3.constructor:查找和bean的构造参数一致的一个或多个bean,若找不到或找到多个,抛异常。按照参数的类型装配

4.autodetect:(3)和(2)之间选一个方式。不确定性的处理与(3)和(2)一致。(选择标准:是否有含参的构造函数,没有则是byType)

<bean id="bar" class="Bar" autowire="byName"/>

<!-- spring2.5 -->

@Autowired

<bean class="... AutowiredAnnotationBeanPostProcessor">

示例代码二: (通过util定义集合bean需要手动添加 spring-util-2.5.xsd)

PopSet.java, javabean

public class PopSet {private String[] ss ;private List list ; private Set set ;   private Map map ;   private Properties prop ;.....省略 get set方法
}

popSet.xml 配置文件, java bean中的 prop 对应到配置文件中用标签<prop> </prop>配置。

<?xml version="1.0"?>
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:util="http://www.springframework.org/schema/util" 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-2.5.xsd http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-2.5.xsd "><bean id="popSet" class="cn.itcast.spring.popset.PopSet"><property name="ss"><list><value>1</value><value>tom</value></list></property><property name="list" ref="myList" /><property name="set"><set><value>1</value><value>tom</value><ref bean="byeService"/><ref bean="byeService"/><bean class="cn.itcast.spring.service.ByeService"><property name="bye" value="kk" /></bean><bean class="cn.itcast.spring.service.ByeService"><property name="bye" value="kk" /></bean></set></property><property name="map"><map><entry key="key001" value="tom" /><entry key="key002" value-ref="byeService" /><entry key="key003"><bean class="cn.itcast.spring.service.ByeService" /></entry></map></property><property name="prop"><props><prop key="key001">tom</prop><prop key="key002">tom1</prop><prop key="key003">tom2</prop></props></property></bean><bean id="byeService" class="cn.itcast.spring.service.ByeService"><property name="bye" value="over" /></bean><!-- 通过util定义集合bean --><util:list id="myList"><value>1</value><value>2</value><value>tom</value><ref bean="byeService"/><ref bean="byeService"/><bean class="cn.itcast.spring.service.ByeService"><property name="bye" value="kk" /></bean><bean class="cn.itcast.spring.service.ByeService"><property name="bye" value="kk" /></bean></util:list>
</beans>

App.java

public class App {public static void main(String[] args) {ApplicationContext ac = new ClassPathXmlApplicationContext("cn/itcast/spring/popset/popSet.xml");ac.getBean("popSet");}
}

4.分散装配

将配置文件分成几个分散的配置文件。如配置数据库的 jdbc.properties文件。使用占位符变量代替bean装配文件中的硬编码配置。占位符采${variable}形式。

定制属性编辑器 , 如下利用自定义的编辑器能将 省市县装配到homeAddress对应的属性中去。

<property name="homeAddress">
            <value>湖南省.长沙市.宁乡县.花明楼镇</value>
</property>

示例代码三:

Address.java, javabean

public class Address {private String province;private String city;private String street;private String zipCode;......省略 set get方法
}

Scatter.java, javabean

public class Scatter {private String driverClass ;private String url ;private String username ;private String password ;private Address comAddress ;//公司地址private Address homeAddress ;//家庭地址}

jdbc.properties 分散配置的 prop文件

jdbc.driverclass=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/test
jdbc.username=root
jdbc.password=root

AddressEditor.java 自定义编辑器

public class AddressEditor extends PropertyEditorSupport {public void setAsText(String text) throws IllegalArgumentException {if(text != null && text.length() > 0){String[] ss = text.split("\\.");if(ss != null && ss.length > 3){Address a = new Address();a.setProvince(ss[0]);a.setCity(ss[1]);a.setStreet(ss[2]);a.setZipCode(ss[3]);//将转换成的地址对象设置给相应的属性上setValue(a);}else{setValue(null);}}else{setValue(null);}}
}

scatter.xml 配置文件

<?xml version="1.0"?>
<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-2.5.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd "><!-- 分散配置(属性占位符配置器)<bean id="propertyPlaceholderConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"><property name="locations"><list><value>classpath:cn/itcast/spring/scatter/jdbc.properties</value></list></property></bean>--><!-- 自定义编辑器配置器 --><bean id="customEditorConfigurer" class="org.springframework.beans.factory.config.CustomEditorConfigurer"><!-- 自定义编辑器集 --><property name="customEditors"><map><entry key="cn.itcast.spring.scatter.Address"><bean class="cn.itcast.spring.editor.AddressEditor" /></entry></map></property></bean><bean id="scatter" class="cn.itcast.spring.scatter.Scatter"><property name="driverClass" value="${jdbc.driverclass}" /><property name="url" value="${jdbc.url}" /><property name="username" value="${jdbc.username}" /><property name="password" value="${jdbc.password}" /><!--  --><property name="comAddress"><bean class="cn.itcast.spring.scatter.Address"><property name="province" value="jilin" /><property name="city" value="cc" /><property name="street" value="${jdbc.url}" /></bean></property><property name="homeAddress"><value>${jdbc.driverclass}.cc.renmin.139988</value></property></bean><!-- 分散配置,加载分散配置的文件 --><context:property-placeholder location="classpath:cn/itcast/spring/scatter/jdbc.properties"/>
</beans>

App.java

public class App {public static void main(String[] args) {ApplicationContext ac = new ClassPathXmlApplicationContext("cn/itcast/spring/scatter/scatter.xml");ac.getBean("scatter");}
}

转载于:https://www.cnblogs.com/xj626852095/p/3648146.html

Spring -- 入门,装备集合,自动装配,分散装配,自定义编辑器相关推荐

  1. Struts2与Spring集成中的自动装配策略

    http://www.blogjava.net/jeffma/archive/2010/11/30/339414.html 自动装配即bean之间的依赖关系无需手动配置. 1.    与自动装配有关的 ...

  2. Spring之Bean的自动装配

    Spring从入门到精通–(4)Spring之Bean的自动装配 作者:进击攻城狮 个人主页:欢迎访问我的主页 首发时间:2022年8月5日星期五 订阅专栏:Spring入门到精通 个人信条:星光不问 ...

  3. Spring入门(二):自动化装配bean

    Spring从两个角度来实现自动化装配: 组件扫描(component scanning):Spring会自动发现应用上下文中需要创建的bean. 自动装配(autowiring):Spring会自动 ...

  4. 详解Spring中Bean的自动装配~

    目录 1. 环境搭建 2. byName.byType 3. 使用注解实现自动装配 @Autowired @Resource 小结 自动装配是Spring满足bean依赖的一种方式 Spring会在上 ...

  5. Spring学习5之自动装配Bean01

    前言 之前我们都是手动装配Bean,但是Spring里面有一个自动装配的方法! 一.自动装配是什么? 自动装配是Spring满足bean依赖一种方式! Spring会在上下文中自动寻找,并自动给bea ...

  6. Spring自动装配----注解装配----Spring自带的@Autowired注解

    Spring自动装配----注解装配----Spring自带的@Autowired注解 父类 package cn.ychx;public interface Person {public void ...

  7. Spring之 Bean的自动装配

    什么是Spring Bean的自动装配? 自动装配是Spring满足bean依赖一种方式 Spring会在上下文种自动寻找,并自动给bean装配属性 Spring种有三种装配方式 1.在xml种显示配 ...

  8. Spring(2)自动装配

    一.装配:Spring在Bean与Bean之间建立依赖关系的行为. 有3种方式: 手动维护:在<constructor-arg>或者<property>标签种通过ref属性进行 ...

  9. Spring依赖注入与自动装配

    Spring依赖注入与自动装配 首先推荐狂神说的Spring讲义 1.Beans.xml作用 简而言之,我们通过在beans.xml中进行配置,将各种类交给spring来管理. 2.依赖注入 推荐狂神 ...

最新文章

  1. android盒子模拟器,emubox模拟器盒子
  2. 马云:我看到很多人去学MBA 但回来时都变蠢了
  3. java 通配符 类_关于类:具有多个类的Java泛型通配符
  4. 使用HttpHandler解析并展示PDF文档内容
  5. Java高效读取大文件(转)
  6. 市直系统推荐市级以上表彰的_推荐市级以及以上教学方面表彰的细则
  7. 小丑马戏团风格英文404网页模板
  8. elementui Cascader 省市区联动选择器,应用与回显
  9. ADO.NET三个基本对象(一)
  10. java中一个char_java 中一个char包含几个字节
  11. amend用法 git 信息_Git 高级用法,你用过哪些了
  12. 常用模块(collections模块,时间模块,random模块,os模块,sys模块,序列化模块,re模块,hashlib模块,configparser模块,logging模块)...
  13. 【备注】【C14】《HeadFirstJava(中文版)(第2版)》 PDF 下载
  14. 正则方程手写(初步实现)
  15. 磊科路由器信号按键_磊科路由器信号增强怎么设置方法
  16. virt-manager 键盘错位解决
  17. 【HTTP】HTPP学习笔记
  18. 斐波那契数列之不死神兔
  19. 真正免费的pdf转word在线工具
  20. 10条不可不知的手机礼仪 看看你犯过哪几项?

热门文章

  1. 协议圣经 -协议之服务编写(九)
  2. 输入你的密码来连接到_手机怎样连接WiFi?详细步骤,教你操作
  3. 摄像头图像分析目标物体大小位置_对智能驾驶系统三种环境传感器布局的冗余关系分析...
  4. 软考路:2021年系统架构设计师之心得
  5. 我的docker随笔7:docker容器与主机之间文件拷贝
  6. qt connect函数_Qt 串口上位机开发Rice 上位机 学习开发
  7. 05-BIO,NIO,AIO几种通讯模式的比较
  8. 【elasticsearch】ES 相似文章检测
  9. 【Elasticsearch】es mapper_parsing_exception
  10. 95-10-092-启动-TokenManager