您现在的位置是:首页 > 个人日记个人日记
spring-02-配置细节
2019-02-03 17:59:52【个人日记】235人已围观
简介配置spring的一些简单细节
配置细节
Bean生命周期
对象信息
package com.spring;
public class MessageService {
private String message;
MessageService(){
System.out.println("init MessageService()");
}
public void init(){
System.out.println("init...");
}
public void destroy(){
System.out.println("destroy");
}
public String getMessage() {
return message;
}
public void setMessage(String message) {
this.message = message;
}
}
spring-config.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">
<!--id:用于从spring容器获得实例时使用-->
<!--class:需要创建实例化的全限定名-->
<!--property:类中的成员-->
<!--scope是bean的作用域-->
<!--scope属性有singleton单例模式,prototype多例模式-->
<!--默认是单例模式-->
<!--init-method初始化时调用该方法,destroy-method结束时调用该方法-->
<bean id="messageService" init-method="init" destroy-method="destroy" class="com.spring.MessageService" scope="singleton">
<!--name:成员的名称-->
<!--value:对应属性的值-->
<property name="message" value="hello spring"/>
</bean>
</beans>
Junit
package com.spring;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class MessageServiceTest {
@Test
public void setMessage() {
// 读取全局配置文件
ApplicationContext applicationContext = new ClassPathXmlApplicationContext(
"spring-config.xml"
);
// 获取Java对象
MessageService messageService = (MessageService) applicationContext.getBean("messageService");
// MessageService messageService1 = applicationContext.getBean(MessageService.class);
System.out.println(messageService);
//摧毁对象
messageService.destroy();
}
}
控制台输出
D:\Java\bin\java.exe -ea -Didea.test.cyclic.buffer.size=1048576 -javaagent:D:\IntelliJ\lib\idea_rt.jar=5119:D:\IntelliJ\bin -Dfile.encoding=UTF-8 -classpath D:\IntelliJ\lib\idea_rt.jar;D:\IntelliJ\plugins\junit\lib\junit-rt.jar;D:\IntelliJ\plugins\junit\lib\junit5-rt.jar;D:\ideaProjects\spring\target\test-classes;D:\ideaProjects\spring\target\classes;C:\Users\Administrator\.m2\repository\org\springframework\spring-context\4.3.17.RELEASE\spring-context-4.3.17.RELEASE.jar;C:\Users\Administrator\.m2\repository\org\springframework\spring-aop\4.3.17.RELEASE\spring-aop-4.3.17.RELEASE.jar;C:\Users\Administrator\.m2\repository\org\springframework\spring-beans\4.3.17.RELEASE\spring-beans-4.3.17.RELEASE.jar;C:\Users\Administrator\.m2\repository\org\springframework\spring-core\4.3.17.RELEASE\spring-core-4.3.17.RELEASE.jar;C:\Users\Administrator\.m2\repository\commons-logging\commons-logging\1.2\commons-logging-1.2.jar;C:\Users\Administrator\.m2\repository\org\springframework\spring-expression\4.3.17.RELEASE\spring-expression-4.3.17.RELEASE.jar;C:\Users\Administrator\.m2\repository\junit\junit\4.12\junit-4.12.jar;C:\Users\Administrator\.m2\repository\org\hamcrest\hamcrest-core\1.3\hamcrest-core-1.3.jar com.intellij.rt.execution.junit.JUnitStarter -ideVersion5 -junit4 com.spring.MessageServiceTest,setMessage
1月 06, 2019 2:03:18 下午 org.springframework.context.support.ClassPathXmlApplicationContext prepareRefresh
信息: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@2145433b: startup date [Sun Jan 06 14:03:18 CST 2019]; root of context hierarchy
1月 06, 2019 2:03:18 下午 org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
信息: Loading XML bean definitions from class path resource [spring-config.xml]
init MessageService()
init...
com.spring.MessageService@23e84203
destory
Process finished with exit code 0
当创建对象时调用init()当摧毁对象时调用destroy()
Tags: JavaWeb
上一篇: 创建博客心得
下一篇: spring-02-配置细节
相关文章
随机图文
评论区
2024-12-21 20:37:01
站长
没有登录功能是为了方便大家留言,但留言接口现在被恶意攻击,将关闭留言接口,如有疑问,请联系我的QQ 1538933906/同微信