Skip to content

Commit b70fd0d

Browse files
committed
✨ Introducing new features.three method bean lifecycle
1 parent bf5b52c commit b70fd0d

File tree

4 files changed

+114
-0
lines changed

4 files changed

+114
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package com.crossoverjie.spring;
2+
3+
import com.crossoverjie.concurrent.Singleton;
4+
import org.springframework.context.annotation.Bean;
5+
import org.springframework.context.annotation.ComponentScan;
6+
import org.springframework.context.annotation.Configuration;
7+
8+
/**
9+
* Function:
10+
*
11+
* @author crossoverJie
12+
* Date: 19/03/2018 22:37
13+
* @since JDK 1.8
14+
*/
15+
@Configuration
16+
public class LifeCycleConfig {
17+
18+
19+
@Bean(initMethod = "start", destroyMethod = "destroy")
20+
public SpringLifeCycle create(){
21+
SpringLifeCycle springLifeCycle = new SpringLifeCycle() ;
22+
23+
return springLifeCycle ;
24+
}
25+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package com.crossoverjie.spring;
2+
3+
import com.crossoverjie.Application;
4+
import org.slf4j.Logger;
5+
import org.slf4j.LoggerFactory;
6+
import org.springframework.beans.BeansException;
7+
import org.springframework.context.ApplicationContext;
8+
import org.springframework.context.ApplicationContextAware;
9+
10+
/**
11+
* Function:
12+
*
13+
* @author crossoverJie
14+
* Date: 20/03/2018 18:23
15+
* @since JDK 1.8
16+
*/
17+
public class SpringLifeCycle{
18+
19+
private final static Logger LOGGER = LoggerFactory.getLogger(SpringLifeCycle.class);
20+
public void start(){
21+
LOGGER.info("SpringLifeCycle start");
22+
}
23+
24+
25+
public void destroy(){
26+
LOGGER.info("SpringLifeCycle destroy");
27+
}
28+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package com.crossoverjie.spring.annotation;
2+
3+
import com.crossoverjie.spring.SpringLifeCycle;
4+
import org.slf4j.Logger;
5+
import org.slf4j.LoggerFactory;
6+
import org.springframework.stereotype.Component;
7+
8+
import javax.annotation.PostConstruct;
9+
import javax.annotation.PreDestroy;
10+
11+
/**
12+
* Function:
13+
*
14+
* @author crossoverJie
15+
* Date: 20/03/2018 18:46
16+
* @since JDK 1.8
17+
*/
18+
@Component
19+
public class AnnotationBean {
20+
private final static Logger LOGGER = LoggerFactory.getLogger(AnnotationBean.class);
21+
22+
@PostConstruct
23+
public void start(){
24+
LOGGER.info("AnnotationBean start");
25+
}
26+
27+
@PreDestroy
28+
public void destroy(){
29+
LOGGER.info("AnnotationBean destroy");
30+
}
31+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package com.crossoverjie.spring.service;
2+
3+
import com.crossoverjie.spring.SpringLifeCycle;
4+
import org.slf4j.Logger;
5+
import org.slf4j.LoggerFactory;
6+
import org.springframework.beans.factory.DisposableBean;
7+
import org.springframework.beans.factory.InitializingBean;
8+
import org.springframework.stereotype.Component;
9+
import org.springframework.stereotype.Service;
10+
11+
/**
12+
* Function:首先执行
13+
*
14+
* @author crossoverJie
15+
* Date: 20/03/2018 18:38
16+
* @since JDK 1.8
17+
*/
18+
@Service
19+
public class SpringLifeCycleService implements InitializingBean,DisposableBean{
20+
private final static Logger LOGGER = LoggerFactory.getLogger(SpringLifeCycleService.class);
21+
@Override
22+
public void destroy() throws Exception {
23+
LOGGER.info("SpringLifeCycleService destroy");
24+
}
25+
26+
@Override
27+
public void afterPropertiesSet() throws Exception {
28+
LOGGER.info("SpringLifeCycleService start");
29+
}
30+
}

0 commit comments

Comments
 (0)