javaEE颠覆者六

javaEE颠覆者第二章
2.3 Bean的初始化和销毁
简单的例子

(1)pom.xml
增加的依赖
片段


    <dependency>
        <groupId>javax.annotationgroupId>
        <artifactId>jsr250-apiartifactId>
        <version>1.0version>
    dependency>

全篇

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0modelVersion>
  <groupId>com.wiselygroupId>
  <artifactId>spring4artifactId>
  <version>0.0.1-SNAPSHOTversion>
  <properties>
    <java.version>1.8java.version>
  properties>
  <dependencies>
  
    
    <dependency>
        <groupId>org.springframeworkgroupId>
        <artifactId>spring-contextartifactId>
        <version>4.1.6.RELEASEversion>
    dependency>
    
    
    <dependency>
        <groupId>org.springframeworkgroupId>
        <artifactId>spring-aopartifactId>
        <version>4.1.6.RELEASEversion>
    dependency>
    
    <dependency>
        <groupId>org.aspectjgroupId>
        <artifactId>aspectjrtartifactId>
        <version>1.8.5version>
    dependency>
    
    <dependency>
        <groupId>org.aspectjgroupId>
        <artifactId>aspectjweaverartifactId>
        <version>1.8.5version>
    dependency>
    <dependency>  
      <groupId>org.apache.geronimo.bundlesgroupId>  
      <artifactId>aspectjweaverartifactId>  
      <version>1.6.8_2version>  
    dependency>  
    
    
    <dependency>
        <groupId>commons-iogroupId>
        <artifactId>commons-ioartifactId>
        <version>2.3version>
    dependency>
    
    
    <dependency>
        <groupId>javax.annotationgroupId>
        <artifactId>jsr250-apiartifactId>
        <version>1.0version>
    dependency>

  dependencies>
  <build>
       <plugins>
            <plugin>
                <groupId>org.apache.maven.pluginsgroupId>
                <artifactId>maven-comipler-pluginartifactId>
                <version>2.3.2version>
                <configuration>
                    <source>${java.version}source>
                    <target>${java.version}target>
                configuration>
            plugin>
       plugins>
  build>  
project>

(2)是用@Bean形式的Bean

package spring4.prepost;

public class BeanWayService {
    public void init() {
        System.out.println("@Bean-init-method:bean出生了");
    }
    public BeanWayService() {
        super();
        System.out.println("初始化构造参数-BeanWayService");
    }
    public void destroy() {
        System.out.println("@Bean-destory-method");
    }
}

(3)使用JSR250形式的Bean

package spring4.prepost;

import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;

public class JSR250WayService {
    @PostConstruct //在构造函数执行后执行
    public void init() {
        System.out.println("jsr250-init-method");
    }
    public JSR250WayService() {
        super();
        System.out.println("初始化构造函数-JSR250WayService");
    }
    @PreDestroy //在Bean销毁之前执行
    public void destroy() {
        System.out.println("jsr250-destory-method");
    }
}

(4)配置类

package spring4.prepost;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;

@Configuration
@ComponentScan("spring4.prepost")
public class PrePostConfig {
    @Bean(initMethod="init",destroyMethod="destroy") //initMethod和destroyMethod指定BeanWayService类的init和destory方法在构造以后、Bean销毁之前执行
    BeanWayService beanWayService() {
        return new BeanWayService();
    }

    @Bean
    JSR250WayService jsr250WayService() {
        return new JSR250WayService();
    }
}

(5)运行

package spring4.prepost;

import org.springframework.context.annotation.AnnotationConfigApplicationContext;

public class Main {
    public static void main(String[] args) {
        AnnotationConfigApplicationContext context 
                        = new AnnotationConfigApplicationContext(PrePostConfig.class);
        JSR250WayService jsr250WayService = context.getBean(JSR250WayService.class);
        BeanWayService beanWayService = context.getBean(BeanWayService.class);
        context.close();
    }
}

结果
javaEE颠覆者六_第1张图片

你可能感兴趣的:(学习资料)