spring4.0 Bean的装配,初始化,销毁

package com.example.demo;

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

@Configuration
public class MyConfig {

    @Bean("wx")
    //默认情况下是单例的,多例可以像下面那样配
    @Scope("prototype")
    public MyBean CreateMyBean() {
        return new MyBean();

    }



    @Bean(initMethod="init",destroyMethod="destroy")
    public dogBean CreatedogBean() {
        return new dogBean();

    }


    @Bean
    public catBean CreatecatBean() {
        return new catBean();

    }




    @Bean("uuu")
    public User CreateUser() {
        return new User();

    }








}
package com.example.demo;

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

import org.springframework.beans.factory.annotation.Autowired;

public class catBean {


    @Autowired
    User user;

    @PostConstruct
    public void init() throws Exception {

        System.out.println("===========cat  init======================");

    }

    @PreDestroy
    public void destroy() {

        System.out.println("===========cat  destory======================");
    }

}
package com.example.demo;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.annotation.Bean;

@SpringBootApplication
public class DemoApplication {

    @Bean
    public Runnable createRunnable() {
        return () ->{System.out.println("spring boot run");};

    }

    public static void main(String[] args) {
        ConfigurableApplicationContext context = SpringApplication.run(DemoApplication.class, args);

        context.getBean(Runnable.class).run();

    }
}
package com.example.demo;

public class dogBean {


    public void init() throws Exception {

        System.out.println("===========dog  init======================");

    }


    public void destroy() {

        System.out.println("===========dog  destory======================");
    }

}
package com.example.demo;

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

public class MyBean implements InitializingBean ,DisposableBean {


    private String name;



    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }


    //属性设置之后执行
    @Override
    public void afterPropertiesSet() throws Exception {
        // TODO Auto-generated method stub
        System.out.println("===========初始化,属性设置之后执行======================");

    }

    @Override
    public void destroy() throws Exception {
        // TODO Auto-generated method stub
        System.out.println("===========销毁啊======================");
    }
}
package com.example.demo;

import org.apache.catalina.core.ApplicationContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Primary;
import org.springframework.stereotype.Component;

@Component("haha")
@Primary
public class User {




    public void show() {








    }





}
package com.example.demo;


import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;


@RestController
public class wxController {

     @Autowired
        private MyBean mybean;

     @Autowired
        private User user;

        @RequestMapping(value="/add") 
        public MyBean addUser(){
            mybean.setName("xueer");

            user.show();
            return mybean;

        }





}

commons.lang3.StringUtils

StringUtils.join(Object array[],String separator)

将数组以符号或其他字符串为间隔组成新的字符串

Object array[] 需要转换的数组。separator组成新串的间隔符号,如 “,” “|”

1 private static final String[] str = {"1","2","3","4"};
2 String str2 = StringUtils.join(str, "|");
3 
4 System.out.println(str2);

输出结果:1|2|3|4

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值