您的位置:首页 > 编程语言 > Java开发

Spring基础

2016-05-30 00:00 344 查看
摘要: Spring的主要功能;容器创建对象的方式;Bean的生命周期和作用域;注入(IOC);组件扫描;依赖注入

Spring的主要功能;容器创建对象的方式;Bean的生命周期和作用域;注入(IOC);组件扫描;依赖注入

---------------------------------------------------------------------------------------------------------------------------

Spring:一个开源的轻量级的应用开发框架,器目的是简化企业级应用程序开发,降低侵入性.
Spring的主要功能:



--------------------------------------------------------------------------------------------------------------------------
容器创建对象的方式:
1.调用无参构造的方法:
类: public class ExampleBean {
public ExampleBean(){
System.out.println("ExampleBean的无参构造器...");
}
}

.xml: <bean id="eb1" class="container.instantiation.ExampleBean"/>
<bean id="d1" class="java.util.Date" />

测试方法:@Test
//测试容器创建对象的第一种方式(调用无参构造器)
public void test1(){
//启动容器
ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml");
//向容器申请获得对象
ExampleBean eb1 = ac.getBean("eb1",ExampleBean.class);
System.out.println(eb1);
Date date = ac.getBean("d1",Date.class);
System.out.println(date);
}

2.使用静态工厂方法创建对象:
.xml: <!--
使用静态方法创建一个对象.
factory-method属性: 指定要调用的静态方法.
-->
<bean id="cal1" class="java.util.Calendar" factory-method="getInstance"/>

测试方法:@Test
//测试容器创建对象的第二种方式(静态工厂方法)
public void test2(){
ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml");
Calendar cal = ac.getBean("cal1",Calendar.class);
System.out.println(cal);
}

3.实例工厂方法:
.xml: <!--
使用实例工厂方法创建一个对象.
factory-bean属性:要调用的对象的id.
factory-method属性:要调用的方法.
-->
<bean id="cal2" class="java.util.GregorianCalendar"/>
<bean id="time1" factory-bean="cal2" factory-method="getTime"/>

测试方法:@Test
//测试容器创建对象的第三种方式(实例工厂)
public void test3(){
ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml");
Date date = ac.getBean("time1",Date.class);
System.out.println(date);
}
-------------------------------------------------------------------------------------------------------------------------
Bean的生命周期和作用域:
1.生命周期:
.xml: <!--
init-method属性:指定初始化方法.容器在创建好相应的实例后,会立即调用该实例的初始化方法.
destroy-method属性:指定销毁方法,容器在删除实例之前,会调用该实例的销毁方法.
只有当作用域为singleton时,destroy方法才会执行.
-->
<bean id="mb1" class="container.life.MessageBean" init-method="init"
destroy-method="destroy" scope="prototype"/>

2.作用域:
.xml: <!--
scope属性:指定作用域,缺省值为"singleton"(单例),
如果值为"prototype",则每getBean一次,会创建一个新的实例.
lazy-init属性:如果值为true,表示延迟加载,即容器启动之后,不会创建实例,只有当getBean时才会创建.
-->
<bean id="sb1" class="container.life.SomeBean" scope="singleton" lazy-init="true"/>

------------------------------------------------------------------------------------------------------------------------
注入(IOC):
1.set方法注入:
.xml: <!--
name属性:要注入的属性名称.
ref属性:要注入的对象的id.
-->
<bean id="a1" class="ioc.set.A" >
<property name="c" ref="b1"></property>
</bean>
<bean id="b1" class="ioc.set.C"/>

2.构造器注入:
.xml: <!--
index属性:从0开始,指定参数的位置(即要给第几个参数赋值).
-->
<bean id="wt1" class="ioc.constructer.Waiter"/>
<bean id="rs1" class="ioc.constructer.Restaurant">
<constructor-arg index="0" ref="wt1"></constructor-arg>
</bean>

3.自动装配(autowire)注入:
.xml: <bean id="wt" class="ioc.auto.Waiter"/>
<!--
byName:查找id等于Bean属性名称的bean,然后调用set方法注入.
注:本Bean应有无参构造和set方法.
-->
<bean id="rest" class="ioc.auto.Restaurant" autowire="byName"/>
<!--
byType:查找id等于Bean属性类型的bean,然后调用set方法注入.
注:本Bean应有无参构造和set方法.
-->
<bean id="rest2" class="ioc.auto.Restaurant2" autowire="byType" />

<!--
constructor属性值:查找与属性类型一致的Bean,然后调用构造器完成注入.
-->
<bean id="rest3" class="ioc.auto.Restaurant3" autowire="constructor"/>

4.参数值注入:
类: public class ExampleBean {
private String name;
private int age;
private List<String> cities;
private Set<String> interests;
private Map<String,Double> scores;
private Properties db;
........它们的get,set方法.....
}
.xml:
<bean id="eb1" class="ioc.basic.ExampleBean">
<property name="name" value="黛玉"/>
<property name="age" value="16"/>
<property name="cities">
<list>
<value>合肥</value>
<value>亳州</value>
<value>亳州</value>
<value>安庆</value>
</list>
</property>
<property name="interests">
<set>
<value>足疗</value>
<value>按摩</value>
<value>大宝剑</value>
</set>
</property>
<property name="scores">
<map>
<entry key="english" value="60"/>
<entry key="模电" value="99"/>
</map>
</property>
<property name="db">
<props>
<prop key="username">种子</prop>
<prop key="password">太阳花</prop>
</props>
</property>
</bean>

引用Bean来注入参数:
.xml; <!--
将集合类型当作一个Bean来配置,这样集合类型的值就可以重用.
命名空间 区分同名元素
-->
<util:list id="citiesBean">
<value>合肥</value>
<value>亳州</value>
<value>无锡</value>
</util:list>
<util:set id="interestBean">
<value>足疗</value>
<value>按摩</value>
<value>大宝剑</value>
</util:set>
<util:map id="scoreBean">
<entry key="english" value="60"/>
<entry key="模电" value="99"/>
</util:map>
<bean id="eb2" class="ioc.basic.ExampleBean">
<property name="cities" ref="citiesBean"/>
<property name="interests" ref="interestBean"/>
<property name="scores" ref="scoreBean"></property>
<property name="db" ref="dbBean"/>
</bean>
<util:properties id="dbBean">
<prop key="username">种子</prop>
<prop key="password">太阳花</prop>
</util:properties>
<!--
读取location属性指定位置的文件的内容.
并将内容存放到Properties对象里面.
-->
<util:properties id="jdbc" location="classpath:config.properties"/>

<bean id="sb1" class="ioc.basic.SomeBean">
<property name="name" value="#{eb1.name}"/>
<!--
读取id为eb1的bean的cities属性值(该属性是一个list,
读取下标为1的元素的值)
-->
<property name="city" value="#{eb1.cities[1]}"/>
<!--
读取id为eb1的bean的score属性值(该属性是一个Map,
读取key为english的值)
-->
<property name="score" value="#{eb1.scores.english}"/>
<!--
读取id为jdbc的bean的pageSize属性值(该属性是一个Properties,
读取key为pageSize的值)
-->
<property name="pageSize" value="#{jdbc.pageSize}"/>
</bean>

config.properties: pageSize=10
------------------------------------------------------------------------------------------------------------------------
组件扫描:
- @Component:通用注解
- @Named:通用注解.
- @Repository:持久化层组件注解.
- @Service:业务层组件注解.
- Controller:控制层足迹注解.

.xml: <!--
配置组件扫描
容器会扫描base-package指定的包及其子包下面的所有的类,如果该类前面
有特定的注解(比如@Component),则容器将其纳入(相当在配置文件中有一个bean)
-->
<context:component-scan base-package="annotation"/>

类:
/**
* 演示组件扫描
* 默认的id是首字母小写之后的类名.
* @scope:指定作用域,缺省值为"singleton"(单例)
* @Lazy:延迟加载,值为true时,表示延迟加载
* @author Administrator
*
*/
@Component("eb1")
//@Scope("prototype")
@Lazy(true)
public class ExampleBean {
public ExampleBean(){
System.out.println("ExampleBean无参构造器...");
}
@PostConstruct
//初始化回调方法
//注意:@PostConstruct和@PreDestroy,这两个注解来自javaee
public void init(){
System.out.println("ExampleBean的init化方法...");
}
@PreDestroy
//销毁回调方法
public void destory(){
System.out.println("ExampleBean的destory方法...");
}
}
------------------------------------------------------------------------------------------------------------------------
依赖注入:
一.@Autowired/@Qualifier:可以处理构造器注入和Setter注入,单稍复杂.
/**
* 演示使用@Autowired注解来完成构造器方式的注入.
* @author Administrator
*
*/
@Component("rest")
public class Restaurant {
private Waiter wt;
public Restaurant(){
System.out.println("Restaurant无参构造器...");
}
@Autowired
//@qualifier指定要注入的bean的id
public Restaurant(@Qualifier("waiter")Waiter wt){
System.out.println("Restaurant有参构造器...");
this.wt = wt;
}
@Autowired
public void setWt(@Qualifier("waiter")Waiter wt) {
System.out.println("Restaurant2的set方法...");
this.wt = wt;
}

}

二.@Rresource:只能处理Setter注入,稍简单.
/**
* 演示@Resource注解完成set方式的注入.
* @author Administrator
*
*/
@Component
public class Manager {
private Computer cp;

@Value("#{jdbc.pageSize}")
//使用Value注解来赋值,Value注解可以使用spring表达式.
private int pageSize;

@Value("宝玉")
//Value注解还可以完成基本类型的值的注入.
private String name;

public Manager(){
System.out.println("Manager无参构造器...");
}
public Computer getCp() {
return cp;
}
@Resource(name="computer")
//name属性指定要注入的bean的id
public void setCp(Computer cp) {
System.out.println("Manager的set方法...");
this.cp = cp;
}
}

.xml:
<context:component-scan base-package="annotation"/>
<util:properties id="jdbc" location="classpath:config.properties"/>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: