依然直接上代码,代码才是王道!!!
App.java
package com.mkyong.common;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.mkyong.customer.services.CustomerService;
public class App
{
public static void main( String[] args )
{
ConfigurableApplicationContext context =
new ClassPathXmlApplicationContext(new String[] {"Spring-Customer.xml"});
CustomerService cust = (CustomerService)context.getBean("customerService");
System.out.println(cust);
context.close();
}
}
CustomerService.java
package com.mkyong.customer.services;
import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;
public class CustomerService
{
String message;
public String getMessage() {
return message;
}
public void setMessage(String message) {
this.message = message;
}
@PostConstruct
public void initIt() throws Exception {
System.out.println("Init method after properties are set : " + message);
}
@PreDestroy
public void cleanUp() throws Exception {
System.out.println("Spring Container is destroy! Customer clean up");
}
}
Spring-Customer.xml
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-2.5.xsd">
<context:annotation-config />
<bean id="customerService" class="com.mkyong.customer.services.CustomerService">
<property name="message" value="i'm property message" />
</bean>
</beans>
AppTest.java
package com.mkyong.common;
import junit.framework.Test;
import junit.framework.TestCase;
import junit.framework.TestSuite;
/**
* Unit test for simple App.
*/
public class AppTest
extends TestCase
{
/**
* Create the test case
*
* @param testName name of the test case
*/
public AppTest( String testName )
{
super( testName );
}
/**
* @return the suite of tests being tested
*/
public static Test suite()
{
return new TestSuite( AppTest.class );
}
/**
* Rigourous Test :-)
*/
public void testApp()
{
assertTrue( true );
}
}
分享到:
相关推荐
java @PostConstruct和@PreConstruct注解整理.........
获取springbean对象
NULL 博文链接:https://nhy520.iteye.com/blog/889276
主要介绍了Spring实战之使用@POSTConstruct和@PreDestroy定制生命周期行为操作,结合实例形式详细分析了Spring使用@POSTConstruct和@PreDestroy定制生命周期相关接口定义、配置与功能实现技巧,需要的朋友可以参考下
主要给大家介绍了关于Spring定时任务中@PostConstruct被多次执行异常的分析与解决方法,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧。
主要介绍了SpringBoot @PostConstruct原理用法解析,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
@postConstruct 所标注的方法 内部是靠的spring提供的两个后置处理器(InitDestroyAnnotationBeanPostProcessor 和 CommonAnnotationBeanPostProcessor)共同 协调分布处理完成的。 这2点也是网上绝大部人没讲明白的...
在 spring 框架中,除了使用其特有的注解外,使用基于 JSR-250 的注解,它包括 @PostConstruct, @PreDestroy 和 @Resource 注释。 首先,咱们简单了解 @PostConstruct 和 @PreDestroy 注释: 为了定义一个 bean...
spring 注解初始化,初始化开始@postconstruct 对象销毁@predestroy javax.annotation.jar文件
@JsonPropertyOrder , @JsonSerialize , @JsonSetter , @Lazy , @Order , @PathVariable , @Pointcut , @PostConstruct , @PostMapping , @PreDestroy , @Primary , @PropertySource , @Qualifier , @Repository , ...
Java中 @PostConstruct和@PreDestroy注解有什么作用?编写Webservice接口服务端(框架)有几种方式?常用的缓存框架有哪几个?简述TCP三次握手的理解 Linux下你常用的命令有哪些?Nginx有什么作用?
spring4框架系列 [ 5 ] 基于注释注入@Component @Value @Resource @Autowired @PostConstruct @PreDestroy
@PostConstruct and @PreDestroy. DETAILS Please see the doc at https://github.com/Netflix/governator/wiki BUILDING Governator is built via Gradle (http://www.gradle.org). To build from the command line...
java原始生成 ...通过@PostConstruct和@PreDestory支持生命周期方法 支持@Factory和@Bean 提供API以获取实现接口的所有bean实例 提供API以获取所有具有注释的bean实例 与服务器端Web框架集成Javalin,Helidon
1、通过java提供的@PostConstruct注解; 2、通过实现spring提供的InitializingBean接口,并重写其afterPropertiesSet方法; 3、通过spring的xml bean配置或bean注解指定初始化方法,如下面实例的initMethod方法通过@...
Spring Bean 的生命周期 > Spring Bean 的生命周期简单易懂。在一个 bean 实例被初始化时,需要执行一系列的初 始化操作以达到可用的状态。同样的,当一个 bean 不在被调用...> @PostConstruct 和@PreDestroy 注解方式
使用@PostConstruct 跟随项目服务启动; 异常处理:最大连接数控制; Client方法:连接服务器并发送数据;接收来自服务器的数据;使用@SpringBootTest启动,用于测试连接、数据通信; spring上下文工具类:服务端...
spring MVC 初始启动concurrent blocking queue,通过@PostConstruct 注解实现,详情参考我的博客
注解介绍等详细说明及使用: • @Controller • @Service ... • @PostConstruct • @PreDestroy • @Repository • @Component (不推荐使用) • @Scope • @SessionAttributes • @InitBinder