c语言sscanf函数的用法是什么
253
2022-12-20
spring容器启动实现初始化某个方法(init)
spring容器启动 初始化某方法(init)
1、前言
很多时候,我们需要在项目启动的时候,就要完成某些方法的执行。今天整理了一个简单的方法,使用spring容器中bean的属性:init-method
2、代码
/*
初始化的类。这里不需要添加任何注解
*/
public class InitData {
@Autowired
private UserService userService;
/*
初始化方法
*/
public void inits(){
System.out.println("初始化方法执行.....");
List
System.out.println(userList.toString());
}
}
3、配置
xmlns:xsi="http://w3.org/2001/XMLSchema-instance" xmlns:p="http://springfrahttp://mework.org/schema/p" xmlns:context="http://springframework.org/schema/context" xmlns:mvc="http://springframework.org/schema/mvc" xsi:schemaLocation="http://springframework.org/schema/beans http://springframework.org/schema/beans/spring-beans-3.0.xsd http://springframework.org/schema/context http://springframework.org/schema/context/spring-context-3.0.xsd http://springframework.org/schema/mvc http://springframework.org/schema/mvc/spring-mvc-3.2.xsd" default-lazy-init="true"> <!-http://- 容器启动后执行:需要执行初始化方法,所以必须直接实例化,取消懒加载-->
xmlns:xsi="http://w3.org/2001/XMLSchema-instance"
xmlns:p="http://springfrahttp://mework.org/schema/p"
xmlns:context="http://springframework.org/schema/context"
xmlns:mvc="http://springframework.org/schema/mvc"
xsi:schemaLocation="http://springframework.org/schema/beans http://springframework.org/schema/beans/spring-beans-3.0.xsd
http://springframework.org/schema/context http://springframework.org/schema/context/spring-context-3.0.xsd
http://springframework.org/schema/mvc http://springframework.org/schema/mvc/spring-mvc-3.2.xsd"
default-lazy-init="true">
<!-http://- 容器启动后执行:需要执行初始化方法,所以必须直接实例化,取消懒加载-->
5、结果
6、注意事项
当初始化方法中有依赖注入的时候,需要将加载注入的bean放在初始化bean之前。最好直接放在子容器中。因为父容器先于子容器初始化。否则依赖注入报错。
取消初始化bean的懒加载,否则初始化方法无法执行。
lazy-init 设置只对scop属性为singleton的bean起作用
spring容器启动初始化的几种方法
方法一
实现InitializingBean接口
InitializingBean接口为bean提供了初始化方法的方式,它只包括afterPropertiesSet方法,凡是继承该接口的类,在初始化bean的时候都会执行该方法。
xml中添加bean
不在xml中添加可以在Initializing类头部添加注解@Service
Initializing.java
public class Initializing implements InitializingBean{
private static final Logger logger = LoggerFactory.getLogger(Initializing.class);
// 方法一
@Override
public void afterPropertiesSet() throws Exception {
logger.info(">>>>>>>> init start...");
}
public void init() {
logger.info(">>>>>>>> 初始化...");
}
}
启动项目日志如下:
Invoking afterPropertiesSet() on bean with name 'initializingBean'
>>>>>>>> init start...
Invoking init method 'init' on bean with name 'initializingBean'
>>>>>>>> 初始化...
方法二
使用@PostConstruct注解
@PostConstruct
public void initConstruct() {
System.out.println("注解初始化...");
}
注意:此方法所在的类需要被扫描到,多种注解可以用,推荐使用@Service
执行顺序:方法二比方法一优先执行。
方法三
web.xml配置
FilterServlet.java
public class FilterServlet implements Filter {
@Override
public void init(FilterConfig filterConfig) throws ServletException {
System.out.println("FilterServlet 初始化");
}
@Override
public void doFilter(ServletRequest request, ServletResponse response, FilterChain
chain)
throws IOException, ServletException {
chain.doFilter(request, response);
}
@Override
public void destroy() {
}
}
执行顺序:优先级比上面两个低。
版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。
发表评论
暂时没有评论,来抢沙发吧~