转载

java开发两年,连Spring中bean的装配都不知道?你怎么涨薪啊

Spring

1.1.1.1 创建一个bean

package com.zt.spring;

public class MyBean {

    private String userName;

    private Integer userAge;

}

1.1.1.2 配置Config 配置bean

package com.zt.spring;

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

@Configuration
public class MyBeanConfig {
     // 和xml中配置文件的bean的标签是一样的
    @Bean(name = "beanName")
    public MyBean createBean(){
        return new MyBean();
    }
}

1.1.1.3 配置bean 单例还是多例的

package com.zt.spring;

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

@Configuration
public class MyBeanConfig {

    @Bean(name = "beanName")
    // 默认值是多例的  xml的方式可以在bean的标签上面  设置这个参数
    @Scope("prototype")  
    public MyBean createBean(){
        return new MyBean();
    }

}

1.1.1.4 获取上文,获取bean

package com.zt.spring;

import org.springframework.context.annotation.AnnotationConfigApplicationContext;

public class App {
    public static void main(String[] args) {
        // 获取上下文
        AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(MyBeanConfig.class);
        // 通过bean的那么获取bean
        System.out.println(context.getBean("beanName"));
        // 获取bean的class文件获取
        System.out.println(context.getBean(MyBean.class));
        context.close();
    }
}

1.1.2 通过FactoryBean实现bena的装配

1.1.2.1 创建一个bean

package com.zt.spring;

public class Car {
}

1.1.2.2 配置Config 配置bean

package com.zt.spring;

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

@Configuration
public class MyBeanConfig {

    @Bean
    public CarFactoryBean createRunnableFactoryBean() {
        return new CarFactoryBean();
    }

}

1.1.2.3 配置FactoryBean 生产bean

package com.zt.spring;

import org.springframework.beans.factory.FactoryBean;

public class CarFactoryBean implements FactoryBean<Car> {

    //获取到对应的实体
    @Override
    public Car getObject() throws Exception {
        return new Car();
    }

    // 返回的额对应的是class文件
    @Override
    public Class<?> getObjectType() {
        return Car.class;
    }

    //配置是不是单例
    @Override
    public boolean isSingleton() {
        return true;
    }

}

1.1.2.4 获取上文,获取bean

package com.zt.spring;

import org.springframework.context.annotation.AnnotationConfigApplicationContext;

public class App {
    public static void main(String[] args) {
        // 获取上下文
        AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(MyBeanConfig.class);
        // 通过FactoryBean 创建的bean
        System.out.println(context.getBean(Car.class));
        // 通过name获取car的实体
        System.out.println(context.getBean("createRunnableFactoryBean"));
        context.close();
    }
}

1.1.3 通过另外一种工工厂模式实现bena的装配

1.1.3.1 创建一个bean

package com.zt.spring;

public class Jeep {
}

1.1.3.2 配置Config 配置bean

package com.zt.spring;

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

@Configuration
public class MyBeanConfig {

    @Bean
    public JeepFactory createJeepFactory() {
        return new JeepFactory();
    }

    @Bean
    public Jeep createJeep(JeepFactory factory) {
        return factory.creat();
    }
}

1.1.3.3 创建 Factory 实现构建方法

package com.zt.spring;

public class JeepFactory {

    public Jeep creat() {
        return new Jeep();
    }
}

1.1.3.4 获取上文,获取bean

package com.zt.spring;

import org.springframework.context.annotation.AnnotationConfigApplicationContext;

public class App {
    public static void main(String[] args) {
        // 获取上下文
        AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(MyBeanConfig.class);
        System.out.println(context.getBean("createJeep"));
        System.out.println(context.getBean(Jeep.class));
        context.close();
    }
}

1.2 spring中bean的销毁方法

1.2.1 调用spring自己的初始化和销毁的方法

1.2.1.1 创建一个bean

并且继承InitializingBean, DisposableBean ,实现afterPropertiesSet,destroy方法
package com.zt.spring;

import org.springframework.beans.factory.DisposableBean;
import org.springframework.beans.factory.InitializingBean;

public class User implements InitializingBean, DisposableBean {

    @Override
    public void afterPropertiesSet() throws Exception {
        System.out.println("=======afterPropertiesSet========");
    }

    @Override
    public void destroy() throws Exception {
        System.out.println("=======destroy========");
    }
}

1.2.1.2在conffig文件中装配这个bean

package com.zt.spring;

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

@Configuration
public class MyBeanConfig {

    @Bean
    public User createUser() {
        return new User();
    }
}

1.2.1.3 获取上下文

package com.zt.spring;

import org.springframework.context.annotation.AnnotationConfigApplicationContext;

public class App {
    public static void main(String[] args) {
        // 获取上下文
        AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(MyBeanConfig.class);
        System.out.println(context.getBean(User.class));
        context.close();
    }
}

1.2.2 调用自定义初始化和销毁的方法

1.2.2.1 创建一个bean

自定义init,destroy方法
package com.zt.spring;

public class Dog {
    //  初始化方法
    public void init() throws Exception {
        System.out.println("=======init========");
    }

    // 销毁的方法
    public void destroy() throws Exception {
        System.out.println("=======destroy========");
    }
}

1.2.2.2在conffig文件中装配这个bean

package com.zt.spring;

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

@Configuration
public class MyBeanConfig {

    @Bean(initMethod = "init",destroyMethod = "destroy")
    public Dog createDog() {
        return new Dog();
    }
}

1.2.2.3 获取上下文

package com.zt.spring;

import org.springframework.context.annotation.AnnotationConfigApplicationContext;

public class App {
    public static void main(String[] args) {
        // 获取上下文
        AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(MyBeanConfig.class);
        System.out.println(context.getBean(Dog.class));
        context.close();
    }
}

1.2.3 调用自定义并且注解初始化和销毁的方法

1.2.3.1 创建一个bean

自定义init,destroy方法  在加上注解放 @PostConstruct, @PreDestroy的方式实现
package com.zt.spring;

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

public class UserInfo {

    @PostConstruct
    public void afterPropertiesSet() throws Exception {
        System.out.println("=======afterPropertiesSet========");
    }

    @PreDestroy
    public void destroy() throws Exception {
        System.out.println("=======destroy========");
    }
}

1.2.3.2在conffig文件中装配这个bean

package com.zt.spring;

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

@Configuration
public class MyBeanConfig {
    @Bean
    public UserInfo createUserInfo() {
        return new UserInfo();
    }
}

1.2.2.3 获取上下文

package com.zt.spring;

import org.springframework.context.annotation.AnnotationConfigApplicationContext;

public class App {
    public static void main(String[] args) {
        // 获取上下文
        AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(MyBeanConfig.class);
        System.out.println(context.getBean(UserInfo.class));
        context.close();
    }
}

1.4 pom.文件

<?xml version="1.0" encoding="UTF-8"?>
<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.0</modelVersion>

    <groupId>com..zt</groupId>
    <artifactId>spring</artifactId>
    <version>1.0-SNAPSHOT</version>

    <properties>
        <project.build.sourceEncodding>UTF-8</project.build.sourceEncodding>
        <maven.compiler.source>1.8</maven.compiler.source>
        <maven.compiler.target>1.8</maven.compiler.target>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>4.3.20.RELEASE</version>
        </dependency>
    </dependencies>


</project>

最后

感谢你看到这里,说的都是自己的一些看法和见解,如有不对,请指正!觉得文章对你有帮助的话不妨给我点个赞,每天都会分享java相关技术文章或行业资讯,欢迎大家关注和转发文章!


很遗憾的说,推酷将在这个月底关闭。人生海海,几度秋凉,感谢那些有你的时光。

原文  https://segmentfault.com/a/1190000023355464
正文到此结束
Loading...