您现在的位置是:首页 > 个人日记个人日记

spring-02-配置细节

2019-02-03 17:59:52【个人日记】233人已围观

简介配置spring的一些简单细节

配置细节

Bean生命周期

对象信息

  1. package com.spring;
  2. public class MessageService {
  3. private String message;
  4. MessageService(){
  5. System.out.println("init MessageService()");
  6. }
  7. public void init(){
  8. System.out.println("init...");
  9. }
  10. public void destroy(){
  11. System.out.println("destroy");
  12. }
  13. public String getMessage() {
  14. return message;
  15. }
  16. public void setMessage(String message) {
  17. this.message = message;
  18. }
  19. }

spring-config.xml配置

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <beans xmlns="http://www.springframework.org/schema/beans"
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4. xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  5. <!--id:用于从spring容器获得实例时使用-->
  6. <!--class:需要创建实例化的全限定名-->
  7. <!--property:类中的成员-->
  8. <!--scope是bean的作用域-->
  9. <!--scope属性有singleton单例模式,prototype多例模式-->
  10. <!--默认是单例模式-->
  11. <!--init-method初始化时调用该方法,destroy-method结束时调用该方法-->
  12. <bean id="messageService" init-method="init" destroy-method="destroy" class="com.spring.MessageService" scope="singleton">
  13. <!--name:成员的名称-->
  14. <!--value:对应属性的值-->
  15. <property name="message" value="hello spring"/>
  16. </bean>
  17. </beans>

Junit

  1. package com.spring;
  2. import org.junit.Test;
  3. import org.springframework.context.ApplicationContext;
  4. import org.springframework.context.support.ClassPathXmlApplicationContext;
  5. public class MessageServiceTest {
  6. @Test
  7. public void setMessage() {
  8. // 读取全局配置文件
  9. ApplicationContext applicationContext = new ClassPathXmlApplicationContext(
  10. "spring-config.xml"
  11. );
  12. // 获取Java对象
  13. MessageService messageService = (MessageService) applicationContext.getBean("messageService");
  14. // MessageService messageService1 = applicationContext.getBean(MessageService.class);
  15. System.out.println(messageService);
  16. //摧毁对象
  17. messageService.destroy();
  18. }
  19. }

控制台输出

  1. 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
  2. 1 06, 2019 2:03:18 下午 org.springframework.context.support.ClassPathXmlApplicationContext prepareRefresh
  3. 信息: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@2145433b: startup date [Sun Jan 06 14:03:18 CST 2019]; root of context hierarchy
  4. 1 06, 2019 2:03:18 下午 org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
  5. 信息: Loading XML bean definitions from class path resource [spring-config.xml]
  6. init MessageService()
  7. init...
  8. com.spring.MessageService@23e84203
  9. destory
  10. Process finished with exit code 0

当创建对象时调用init()当摧毁对象时调用destroy()

Tags: JavaWeb  

评论区

    2024-12-12 22:11:46

    站长

    没有登录功能是为了方便大家留言,但留言接口现在被恶意攻击,将关闭留言接口,如有疑问,请联系我的QQ 1538933906/同微信


文章评论



给自个选个头像吧!






站点信息

  • 建站时间:   2019-01-31
  • 网站程序:   Tomcat+nginx
  • 文章统计:   44篇文章
  • 标签管理:   标签云
  • 微信公众号:  扫描二维码,联系我