Spring單例學(xué)習(xí)
1.概述
2.練習(xí)
2.1.接口定義
package com.tech.ability.springstudy.service;
/**
-
Created by kikop on 2019/2/25.
*/
public interface IHelloWorldService {public void sayHello();
}
2.2.接口實(shí)現(xiàn)
package com.tech.ability.springstudy.service.impl;
import com.tech.ability.springstudy.service.IHelloWorldService;
/**
-
Created by kikop on 2019/2/25.
*/
public class SpringHelloWorldImpl implements IHelloWorldService {@Override
public void sayHello() {
System.out.println("SpringHelloWorldImpl");
}
}
2.3.后臺屬性注入
package com.tech.ability.springstudy;
import com.tech.ability.springstudy.service.IHelloWorldService;
/**
- Created by kikop on 2019/2/25.
*/
public class HelloWorldServiceInject {
//單例測試
public int globalVal = 0;
public int getGlobalVal() {
return globalVal;
}
public void setGlobalVal(int globalVal) {
this.globalVal = globalVal;
}
//屬性注入
private IHelloWorldService iHelloWorldService;
public IHelloWorldService getiHelloWorldService() {
return iHelloWorldService;
}
public void setiHelloWorldService(IHelloWorldService iHelloWorldService) {
this.iHelloWorldService = iHelloWorldService;
}
public HelloWorldServiceInject() {
}
}
2.4.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"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd">
<!--這里定義Spring配置文件沦偎,并聲明所有可用的bean-->
<!--1.定義實(shí)現(xiàn) iHelloWorldService接口的bean對象-->
<bean id="springHelloWorldImplSingleTon" class="com.tech.ability.springstudy.service.impl.SpringHelloWorldImpl"></bean>
<bean id="structHelloWorldImpl" class="com.tech.ability.springstudy.service.impl.StructHelloWorldImpl"></bean>
<!--2.引用實(shí)現(xiàn) iHelloWorldService接口的bean對象,默認(rèn)單例Singleton-->
<bean id="helloWorldServiceInjectSingleTon" class="com.tech.ability.springstudy.HelloWorldServiceInject"
scope="singleton">
<property name="iHelloWorldService" ref="springHelloWorldImplSingleTon"></property>
</bean>
<bean id="helloWorldServiceInjectNew" class="com.tech.ability.springstudy.HelloWorldServiceInject"
scope="prototype">
<property name="iHelloWorldService" ref="springHelloWorldImplSingleTon"></property>
</bean>
</beans>
2.5.測試
package com.tech.ability.springstudy;
import com.tech.ability.mycommonutils.DateUtil;
import com.tech.ability.springstudy.service.IHelloWorldService;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
/**
-
Created by kikop on 2019/2/25.
*/
public class MySpringTest {/**
-
spring屬性注入測試
*/
public static void ioctest() {//1.引用xml文件,xml等文件放在resources里面,獨(dú)立于webapp下面的東西挣输。
ApplicationContext context =
new ClassPathXmlApplicationContext("myspring/spring.xml");//2.獲取指定服務(wù)
HelloWorldServiceInject service =
(HelloWorldServiceInject) context.getBean("helloWorldServiceInjectSingleTon");//2.獲取服務(wù)下注入的Bean(SpringHelloWorldImpl、StructHelloWorldImpl)
IHelloWorldService hw = service.getiHelloWorldService();//3.測試
hw.sayHello();
}
-
public static void singletonTest() {
//1.引用xml文件,xml等文件放在resources里面,獨(dú)立于webapp下面的東西帽驯。
ApplicationContext context =
new ClassPathXmlApplicationContext("myspring/spring.xml");
//2.獲取指定服務(wù)
HelloWorldServiceInject service =
(HelloWorldServiceInject) context.getBean("helloWorldServiceInjectSingleTon");
//2.1.測試
service.setGlobalVal(100);
System.out.println(DateUtil.getCurrentThreadInfo(String.valueOf(service.getGlobalVal())));
//3.再次獲取指定服務(wù)
HelloWorldServiceInject service2 =
(HelloWorldServiceInject) context.getBean("helloWorldServiceInjectSingleTon");
System.out.println(DateUtil.getCurrentThreadInfo(String.valueOf(service2.getGlobalVal())));
}
public static void prototypeTest() {
//1.引用xml文件,xml等文件放在resources里面,獨(dú)立于webapp下面的東西悼瘾。
ApplicationContext context =
new ClassPathXmlApplicationContext("myspring/spring.xml");
//2.獲取指定服務(wù)
HelloWorldServiceInject service =
(HelloWorldServiceInject) context.getBean("helloWorldServiceInjectNew");
//2.1.測試
service.setGlobalVal(100);
System.out.println(DateUtil.getCurrentThreadInfo(String.valueOf(service.getGlobalVal())));
//3.再次獲取指定服務(wù)
HelloWorldServiceInject service2 =
(HelloWorldServiceInject) context.getBean("helloWorldServiceInjectNew");
System.out.println(DateUtil.getCurrentThreadInfo(String.valueOf(service2.getGlobalVal())));
}
public static void main(String[] args) {
System.out.println("singletonTest");
singletonTest();
System.out.println("prototypeTest");
prototypeTest();
}
}
2.6.結(jié)果查看
singletonTest
2019-04-25 07:40:53.092 INFO [main][AbstractApplicationContext.java:589] - Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@21a947fe: startup date [Thu Apr 25 07:40:53 CST 2019]; root of context hierarchy
2019-04-25 07:40:53.148 INFO [main][XmlBeanDefinitionReader.java:316] - Loading XML bean definitions from class path resource [myspring/spring.xml]
[2019-04-25 07:40:53] main:100
[2019-04-25 07:40:53] main:100
prototypeTest
2019-04-25 07:40:53.582 INFO [main][AbstractApplicationContext.java:589] - Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@2e55dd0c: startup date [Thu Apr 25 07:40:53 CST 2019]; root of context hierarchy
2019-04-25 07:40:53.583 INFO [main][XmlBeanDefinitionReader.java:316] - Loading XML bean definitions from class path resource [myspring/spring.xml]
[2019-04-25 07:40:53] main:100
[2019-04-25 07:40:53] main:0
Process finished with exit code 0
參考
1.實(shí)戰(zhàn)Java高并發(fā)程序設(shè)計