Bean life cycle (3)

Methods for configuring Bean initialization and destruction:

Configure initialization and destruction methods:

 * init-method=”setup”

 * destroy-method=”teardown”


When performing destruction, the factory must be manually closed, and it is only valid for scope=” singleton ”.


##### The 11 steps of the Bean's life cycle:

1. instantiate bean object instantiation

2. populate properties

3. If the Bean implements BeanNameAware, execute setBeanName

4. If the Bean implements BeanFactoryAware or ApplicationContextAware, set the factory setBeanFactory or the context object setApplicationContext

5. If there is a class that implements BeanPostProcessor (post-processing Bean), execute postProcessBeforeInitialization

6. If the Bean implements InitializingBean, execute afterPropertiesSet 

7. Call <bean init-method="init"> to specify the initialization method init

8. If there is a class implementing BeanPostProcessor (processing Bean), execute postProcessAfterInitialization

9. Execute business processing

10. If the Bean implements DisposableBean, execute destroy

11. Call <bean destroy-method="customerDestroy"> to specify the destroy method customerDestroy


Perform permission verification before the add method of the CustomerService class?


<!-- demo4 bean life cycle -->
<bean id="customerservice" class="cn.spring.demo4.CustomerServiceimpl" init-method="setup" destroy-method="teardown">
<property name="name" value="<property name="name" value="">"/>
</bean>
<bean class="cn.spring.demo4.myBeanPostProcessor"></bean>
<!-- demo4 bean life cycle -->


CustomerService.java interface function
package cn.spring.demo4;
public interface CustomerService {
public void add();
public void find();
}


CustomerServiceimpl.java
package cn.spring.demo4;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.BeanNameAware;
import org.springframework.beans.factory.DisposableBean;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
public class CustomerServiceimpl implements CustomerService,BeanNameAware,ApplicationContextAware,InitializingBean,DisposableBean   {
private String name;
//implement your own business logic
public void add() {
System.out.println("Add user in step 9");
}
public void find() {
System.out.println("Step 9 Find users");
}
public void setName(String name){
System.out.println("Step 2: Package property"+name);
this.name = name;
}
public CustomerServiceimpl() {
super();
// TODO Auto-generated constructor stub
System.out.println("Step 1: Object instantiation");
}
public void setBeanName(String name) {
// TODO Auto-generated method stub
System.out.println("Step 3: Inject configuration file name"+name);
}
public void setApplicationContext(ApplicationContext applicationContext)
throws BeansException {
// TODO Auto-generated method stub
System.out.println("Step 4: Inject ApplicationContext"+applicationContext);
}
public void afterPropertiesSet() throws Exception {
// TODO Auto-generated method stub
System.out.println("Step 6: Execute after attribute setting...");
}
public void setup(){
System.out.println("Step 7: Call the manually set initialization method");
}
public void destroy() throws Exception {
// TODO Auto-generated method stub
System.out.println("Step 10: Destruction method called");
}
public void teardown(){
System.out.println("Step 11: Call the manually set destroy method");
}
}


myBeanPostProcessor.java
package cn.spring.demo4;
import java.lang.reflect.Method;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.BeanPostProcessor;
import org.springframework.cglib.proxy.InvocationHandler;
import org.springframework.cglib.proxy.Proxy;
public class myBeanPostProcessor implements BeanPostProcessor {
/*
 * @bean: instance object
 * 
 * @beanName: The identifier of the class configured in the configuration file
 */
public Object postProcessBeforeInitialization(Object bean, String beanName)
throws BeansException {
// can be enhanced here
System.out.println("Step 5, before initialization");
return bean;
}
public Object postProcessAfterInitialization(final Object bean,
String beanName) throws BeansException {
System.out.println("Step 8: Execute after initialization...");
// Dynamic proxy:
if (beanName.equals("customerservice")) {
Object proxy = Proxy.newProxyInstance(bean.getClass()
.getClassLoader(), bean.getClass().getInterfaces(),
new InvocationHandler() {
// When calling the target method, call the invoke method.
public Object invoke(Object proxy, Method method,
Object[] args) throws Throwable {
if ("add".equals(method.getName())) {
System.out.println("Permission check...");
Object result = method.invoke(bean, args);
System.out.println(System.currentTimeMillis());
return result;
}
return method.invoke(bean, args);
}
});
return proxy;
}
return bean;
}
}


package cn.spring.demo4;
import org.junit.Test;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class SpringTest4 {
@Test
public void demo1() {
ClassPathXmlApplicationContext applicationcontext = new ClassPathXmlApplicationContext(
"applicationContext.xml");
CustomerService customerservice=(CustomerService) applicationcontext.getBean("customerservice");
customerservice.add();
customerservice.find();
applicationcontext.close();
}
}


Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324826623&siteId=291194637