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

Spring入门01

2016-06-29 13:01 567 查看
一、bean的实例化

<!-- 构造器 实例化 -->
<bean id = "demo1" class = "hateapple.entity.Demo1"  ></bean>
<bean id = "demo2" class = "hateapple.entity.Demo2"></bean>
<!-- 静态工厂实例化 -->
<bean id = "demo3" class = "hateapple.entity.Demo2" factory-method = "getDemo2"></bean>
<!-- 实例工厂实例化 -->
<bean id = "demo4" class = "hateapple.entity.Demo2" factory-bean = "demo1" factory-method = "getDemo2"></bean>


Demo1

package hateapple.entity;
public class Demo1 {
private String remark;
public String getRemark() {
return remark;
}
public void setRemark(String remark) {
this.remark = remark;
}
public Demo2 getDemo2(){
System.out.println("由Demo1通过getDemo2方法创建Demo2");
return new Demo2();
}
}


Demo2

package hateapple.entity;
public class Demo2 {
private String remark;
public Demo2(){
//System.out.println("调用无参构造函数创建对象");
}
public static Demo2 getDemo2(){
System.out.println("调用静态工厂创建实例  ");
return new Demo2();
}
public String getRemark() {
return remark;
}
public void setRemark(String remark) {
this.remark = remark;
}
}


二、控制bean的实例化

Demon3

package hateapple.entity;
public class Demo3 {
private String remark;

public String getRemark() {
return remark;
}
public void setRemark(String remark) {
this.remark = remark;
}
public void demo3InitMethod(){
System.out.println("bean实例化后调用的初始化方法");
}
public void demo3DestroyMethod(){
System.out.println("bean被销毁后调用的方法,只对单例模式生效");
}
}


Bean的配置

<!-- scope: 默认为singleton, 除非该bean有父bean, 那么该bean会继承父bean的scope
singleton: 针对同一个beanid的请求只建立一个实例共享(即单例模式);
prototype: 每次请求都会返回一个新的实例对象;
init-method: 对象被创建后调用的自定义方法;
destroy-method: 对象被销毁后调用的自定义方法(只针对singleton模式生效);
lazy-init:
true: 延时创建对象(用到的时候才创建);
false: 容器启动时就创建;
init-method、destroy-method、lazy-init在beans属性上对应的分别有default-init-method、default-destroy-method、
default-lazy-init对所有bean生效
-->
<bean id = "demo5" class = "hateapple.entity.Demo3" scope = "singleton"
init-method = "demo3InitMethod" destroy-method = "demo3DestroyMethod">
</bean>


测试:

package hateapple.test;

import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.AbstractXmlApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import hateapple.entity.Demo3;

public class TestCase {

@Test
public void testInitContext() throws InterruptedException{
String conf = "applicationContext.xml";
//初始化spring 容器
ApplicationContext ac = new ClassPathXmlApplicationContext(conf);
//二、Bean的控制
Demo3 demo1 = (Demo3)ac.getBean("demo5");//测试demo1InitMethod()
//测试scope
Demo3 demo2 = (Demo3)ac.getBean("demo5");
System.out.println("修改scope测试两次getBean得到的实例是否相同 : " + (demo1 == demo2));
Thread.sleep(1000);
AbstractXmlApplicationContext aac = (AbstractXmlApplicationContext)ac;
aac.close();//关闭容器,测试demo1DestroyMethod(),scope必须为singleton

}
}


三、bean属性的注入

基本注入

Demo3:

package hateapple.entity;

public class Demo3 {
private String remark;
private Demo1 demo1;

public Demo3(String remark){
this.remark = remark;
}

public Demo3(Demo1 demo){
this.demo1 = demo;
}
public Demo3(String remark, Demo1 demo){
this.remark = remark;
this.demo1 = demo;
}

public Demo3(){

}
public Demo1 getDemo1() {
return demo1;
}
public void setDemo1(Demo1 demo1) {
this.demo1 = demo1;
}
public String getRemark() {
return remark;
}
public void setRemark(String remark) {
this.remark = remark;
}
public void demo3InitMethod(){
System.out.println("bean实例化后调用的初始化方法");
}
public void demo3DestroyMethod(){
System.out.println("bean被销毁后调用的方法,只对单例模式生效");
}
}


bean配置:

<bean id = "demo6" class = "hateapple.entity.Demo3" >
<!-- 构造器参数注入 -->
<constructor-arg name = "remark">
<value>构造器参数注入</value>
</constructor-arg>
</bean>

<bean id = "demo7" class = "hateapple.entity.Demo3" >
<constructor-arg name = "demo" ref ="demo1">
</constructor-arg>
</bean>

<bean id = "demo8" class = "hateapple.entity.Demo3" >
<constructor-arg index = "0" value = "构造器参数注入"> </constructor-arg>
<constructor-arg index = "1">
<bean class = "hateapple.entity.Demo1"></bean>
</constructor-arg>
</bean>
<!-- autowire:
no:禁止自动注入 ,默认;
byName:根据名字找到属性完全一致的bean装配;
byType:根据制定类型在容器中找到bean,装配;
constructor:根据类型找到bean,使用构造器参数装配;
autodetect:根据目标bean判断使用byType,还是constructor装配;
-->
<bean id = "demo9" class = "hateapple.entity.Demo3" autowire = "byType">

</bean>


测试:

package hateapple.test;

import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import hateapple.entity.Demo3;

public class TestCase {

@Test
public void testInitContext() throws InterruptedException{
String conf = "applicationContext.xml";
//初始化spring 容器
ApplicationContext ac = new ClassPathXmlApplicationContext(conf);
//三、bean属性注入
Demo3 demo1 = (Demo3)ac.getBean("demo5");
System.out.println("setter注入  " + demo1.getRemark() + "    " + demo1.getDemo1());
Demo3 demo2 = (Demo3)ac.getBean("demo6");
System.out.println("构造器参数注入1  " + demo2.getRemark() + "    " + demo2.getDemo1());
Demo3 demo3 = (Demo3)ac.getBean("demo7");
System.out.println("构造器参数注入2  " + demo3.getRemark() + "    " + demo3.getDemo1());
Demo3 demo4 = (Demo3)ac.getBean("demo8");
System.out.println("构造器参数注入3  " + demo4.getRemark() + "    " + demo4.getDemo1());
Demo3 demo5 = (Demo3)ac.getBean("demo9");
System.out.println("autowire注入  " + demo5.getRemark() + "    " + demo5.getDemo1());

}
}


2.集合属性注入

Demo4:

package hateapple.entity;

import java.util.List;
import java.util.Map;
import java.util.Properties;
import java.util.Set;

public class Demo4 {
private List<String> strList;
private Set<String> strSet;
private Map<String, Object> strMap;
private Properties somPros;
public List<String> getStrList() {
return strList;
}
public void setStrList(List<String> strList) {
this.strList = strList;
}
public Set<String> getStrSet() {
return strSet;
}
public void setStrSet(Set<String> strSet) {
this.strSet = strSet;
}
public Map<String, Object> getStrMap() {
return strMap;
}
public void setStrMap(Map<String, Object> strMap) {
this.strMap = strMap;
}
public Properties getSomPros() {
return somPros;
}
public void setSomPros(Properties somPros) {
this.somPros = somPros;
}
}


applicationContext.xml

<bean id = "demo10" class = "hateapple.entity.Demo4">
<property name ="strList">
<list>
<value>张三</value>
<value>李四</value>
<value>王五</value>
</list>
</property>

<property name ="strSet">
<set>
<value>张三</value>
<value>李四</value>
<value>王五</value>
</set>
</property>

<property name ="strMap">
<map>
<entry key = "a" value = "张三"></entry>
<entry key = "b" value = "李四"></entry>
<entry key = "c" value = "王五"></entry>
</map>
</property>

<property name="somPros">
<props>
<prop key="name">hateapple</prop>
<prop key="pwd">123</prop>
</props>
</property>
</bean>


测试:

package hateapple.test;

import java.util.Set;

import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import hateapple.entity.Demo4;

public class TestCase {

@Test
public void testInitContext() throws InterruptedException{
String conf = "applicationContext.xml";
//初始化spring 容器
ApplicationContext ac = new ClassPathXmlApplicationContext(conf);
//三、bean属性注入2
Demo4 demo10 = (Demo4)ac.getBean("demo10");
System.out.println("List信息如下-------");
for (String str : demo10.getStrList()) {
System.out.println(str);
}

System.out.println("Set信息如下-------");
for (String str : demo10.getStrSet()) {
System.out.println(str);
}

System.out.println("Map信息如下-------");
Set<String> keySet = demo10.getStrMap().keySet();
for (String str : keySet) {
System.out.println(str + "  " + demo10.getStrMap().get(str));
}

System.out.println("pros信息如下-------");
Set<Object> prosKeySet = demo10.getSomPros().keySet();
for (Object str : prosKeySet) {
System.out.println(str + " = " + demo10.getSomPros().get(str));
}
}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: