Spring学习(三)

Bean作用域

1
2
3
4
5
6
7
8
package com.xiaoguan.bean;

public class SpringBean {
public SpringBean() {
System.out.println("spring的无参数构造方法执行了");
}
}

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
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">
<bean class="org.springframework.beans.factory.config.CustomScopeConfigurer">
<property name="scopes">
<map>
<entry key="myThread" >
<bean class="org.springframework.context.support.SimpleThreadScope"></bean>
</entry>
</map>
</property>
</bean>
<bean id="sb" class="com.xiaoguan.bean.SpringBean" scope="myThread"></bean>
<!--web项目中还有request和session请求域request一次请求中一个bean,session一次会话中一个bean-->
</beans>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
package com.xiaoguan.test;

import com.xiaoguan.bean.SpringBean;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class ScopeTest {
@Test
public void testThreadScope(){
ApplicationContext applicationContext=new ClassPathXmlApplicationContext("spring-scope.xml");
SpringBean sb = applicationContext.getBean("sb", SpringBean.class);
System.out.println(sb);
new Thread(new Runnable() {
@Override
public void run() {
SpringBean sb1 = applicationContext.getBean("sb", SpringBean.class);
System.out.println(sb1);
}
}).start();
}
@Test
public void testBeanScope(){
ApplicationContext applicationContext=new ClassPathXmlApplicationContext("spring-scope.xml");
SpringBean sb = applicationContext.getBean("sb", SpringBean.class);
System.out.println(sb);
SpringBean sb2 = applicationContext.getBean("sb", SpringBean.class);
System.out.println(sb2);
SpringBean sb3 = applicationContext.getBean("sb", SpringBean.class);
System.out.println(sb3);
}
}

工厂模式

1
2
3
4
5
6
7
8
9
package com.xiaoguan.factorymethod;

public class Dogger extends Weapon{
@Override
public void attack() {
System.out.println("砍丫的");
}
}

1
2
3
4
5
6
7
8
9
package com.xiaoguan.factorymethod;

public class DoggerFactory extends WeaponFactory{
@Override
public Weapon get() {
return new Dogger();
}
}

1
2
3
4
5
6
7
8
9
package com.xiaoguan.factorymethod;

public class Gun extends Weapon{
@Override
public void attack() {
System.out.println("开火");
}
}

1
2
3
4
5
6
7
8
9
package com.xiaoguan.factorymethod;

public class GunFactory extends WeaponFactory{
@Override
public Weapon get() {
return new Gun();
}
}

1
2
3
4
5
6
7
8
9
10
package com.xiaoguan.factorymethod;

public class Test {
public static void main(String[] args) {
WeaponFactory factory=new DoggerFactory();
Weapon weapon = factory.get();
weapon.attack();
}
}

1
2
3
4
5
6
package com.xiaoguan.factorymethod;

abstract public class Weapon {
public abstract void attack();
}

1
2
3
4
5
6
package com.xiaoguan.factorymethod;

abstract public class WeaponFactory {
public abstract Weapon get();
}

1
2
3
4
5
6
7
8
9
package com.xiaoguan.simpleFactory;

public class Dagger extends Weapon{
@Override
public void attack() {
System.out.println("砍丫的");
}
}

1
2
3
4
5
6
7
8
9
package com.xiaoguan.simpleFactory;

public class Fighter extends Weapon{
@Override
public void attack() {
System.out.println("战斗机发射");
}
}

1
2
3
4
5
6
7
8
9
package com.xiaoguan.simpleFactory;

public class Tank extends Weapon{
@Override
public void attack() {
System.out.println("坦克开炮");
}
}

1
2
3
4
5
6
7
8
9
10
package com.xiaoguan.simpleFactory;

public class Test {
public static void main(String[] args) {
Weapon tank = WeaponFactory.get("Tank");
tank.attack();

}
}

1
2
3
4
5
6
package com.xiaoguan.simpleFactory;

public abstract class Weapon {
public abstract void attack();
}

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
package com.xiaoguan.simpleFactory;

public class WeaponFactory {
public static Weapon get(String weanponType){
if ("Tank".equals(weanponType)) {
return new Tank();
}else if ("Fighter".equals(weanponType)){
return new Fighter();
} else if ("Dagger".equals(weanponType)) {
return new Dagger();
}else {
throw new RuntimeException("不支持该武器生产");
}
}
}

Bean实例化

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
package com.xiaoguan.bean;

import org.springframework.beans.factory.FactoryBean;

import java.text.SimpleDateFormat;
import java.util.Date;

public class DataFactoryBean implements FactoryBean<Date> {
private String strDate;
public DataFactoryBean(String strDate) {
this.strDate = strDate;
}

@Override
public Date getObject() throws Exception {
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss SS");
Date parse = simpleDateFormat.parse(strDate);
return parse;
}

@Override
public Class<?> getObjectType() {
return null;
}
}

1
2
3
4
5
6
7
8
package com.xiaoguan.bean;

public class Gun {
public Gun() {
System.out.println("gun的无参数构造方法执行了");
}
}

1
2
3
4
5
6
7
8
package com.xiaoguan.bean;

public class GunFactory {
public Gun get(){
return new Gun();
}
}

1
2
3
4
5
6
7
8
package com.xiaoguan.bean;

public class Person {
public Person() {
System.out.println("Person的构造方法执行了");
}
}

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
package com.xiaoguan.bean;

import org.springframework.beans.factory.FactoryBean;

public class PersonFactoryBean implements FactoryBean<Person> {
@Override
public Person getObject() throws Exception {
return new Person();
}

@Override
public Class<?> getObjectType() {
return null;
}
//默认已经实现,表示单例,如果不想单例可以改为return false
@Override
public boolean isSingleton() {
return true;
}
}

1
2
3
4
5
6
7
8
package com.xiaoguan.bean;

public class SpringBean {
public SpringBean() {
System.out.println("SpringBean的无参数构造方法执行了");
}
}

1
2
3
4
5
6
7
8
package com.xiaoguan.bean;

public class Star {
public Star() {
System.out.println("start的无参数构造方法执行了");
}
}

1
2
3
4
5
6
7
8
package com.xiaoguan.bean;

public class StarFactory {
public static Star get(){
return new Star();
}
}

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
package com.xiaoguan.bean;

import java.util.Date;

public class Student {
private Date birth;

@Override
public String toString() {
return "Student{" +
"birth=" + birth +
'}';
}

public void setBirth(Date birth) {
this.birth = birth;
}
}

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
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">
<!-- Spring提供实例化方式的第一种,在spring配置文件中直接配置全路径,spring会自动调用该类的无参数构造方法来实例化bean
1.通过构造方法来实例化
2.通过简单工厂模式实例化
3.通过factory-bean实例化
4.通过FactoryBean接口实例化-->
<bean id="sb" class="com.xiaoguan.bean.SpringBean"/>
<!-- 通过简单工厂模式实例化-->
<bean id="sf" class="com.xiaoguan.bean.StarFactory" factory-method="get"/>
<!-- 通过factory-bean实例化-->
<bean id="gunFactory" class="com.xiaoguan.bean.GunFactory"/>
<bean id="gun" factory-bean="gunFactory" factory-method="get"/>
<!-- 通过FactoryBean接口实例化-->
<bean id="person" class="com.xiaoguan.bean.PersonFactoryBean"/>
<!--练习-->
<bean id="student" class="com.xiaoguan.bean.Student">
<!-- <property name="birth" value="Mon Oct 10 14:30:26 CST 2022"/>-->
<property name="birth" ref="nowTime"/>
</bean>
<bean id="nowTime" class="java.util.Date"/>
<bean id="strDate" class="com.xiaoguan.bean.DataFactoryBean">
<constructor-arg index="0" value="1980-10-11 20:30:30 40"/>
</bean>
<bean id="students" class="com.xiaoguan.bean.Student" >
<property name="birth" ref="strDate"/>
</bean>
</beans>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
package com.xiaoguan.test;

import com.xiaoguan.bean.*;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class BeanInstatiation {
@Test
public void testInstatiation5() {
ApplicationContext applicationContext = new ClassPathXmlApplicationContext("spring.xml");
Student students = applicationContext.getBean("students", Student.class);
System.out.println(students);
}
@Test
public void testInstatiation4(){
ApplicationContext applicationContext=new ClassPathXmlApplicationContext("spring.xml");
Person person = applicationContext.getBean("person", Person.class);
System.out.println(person);
}
@Test
public void testInstatiation3(){
ApplicationContext applicationContext=new ClassPathXmlApplicationContext("spring.xml");
Gun gun = applicationContext.getBean("gun", Gun.class);
System.out.println(gun);
}
@Test
public void testInstatiation2(){
ApplicationContext applicationContext=new ClassPathXmlApplicationContext("spring.xml");
Star st = applicationContext.getBean("sf", Star.class);
System.out.println(st);
}
@Test
public void testInstatiation1(){
ApplicationContext applicationContext=new ClassPathXmlApplicationContext("spring.xml");
SpringBean sb = applicationContext.getBean("sb", SpringBean.class);
System.out.println(sb);
}
}