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

xiaoxiao2021-02-28  51

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

转载请注明原文地址: https://www.6miu.com/read-2631910.html

最新回复(0)