首页 技术 正文
技术 2022年11月15日
0 收藏 984 点赞 4,573 浏览 2062 个字

一:spring组件扫描

可以使用注解的方式,代替在xml配置文件配置bean,可以减少配置文件的书写,只需要在spring容器配置

文件中配置<context:component-scan base-package=”com.hlcui.*”/>

但是不是只有扫描,所在包以及子包下的类都会被扫描进去,而是只有类上面标记注解的才会被扫描进spring容器

常见注解:
@Component  通用注解,  一般情况下不确定属于那一层时使用,它仅仅是将类对象扫描到spring容器中

@Repository   持久层注解,放在dao那一层

@Service        业务层注解,放在service那一层

@Controller     控制车注解,放在控制层

下面示例演示:

1:新建TestBean类,在类上面添加注解 @Component

 /**
*
*/
package com.hlcui.dao; import org.springframework.stereotype.Component; /**
* @author Administrator
*
*/
@Component
public class TestBean {
public TestBean() {
System.out.println("实例化bean...");
} public void execute() {
System.out.println("执行bean处理...");
}
}

2:在配置文件中添加

 <!-- 组件扫描 -->
<context:component-scan base-package="com.hlcui"/>

3:测试方法

 @Test
/**测试组件扫描*/
public void testTestBean(){
ApplicationContext ac = getApplicationContext();
TestBean tb = ac.getBean("testBean", TestBean.class);
tb.execute();
}

Spring学习总结五——SpringIOC容器五

通过结果可以看出bean对象被扫描进了spring容器!

二:控制bean的实例化

1:创建ExampleBean1类,并且在类上面添加注解标记@Component

/**
*
*/
package com.hlcui.dao;
import org.springframework.stereotype.Component;/**
* @author Administrator
*
*/
@Component
public class ExampleBean1 {
public ExampleBean1() {
System.out.println("实例化ExampleBean1...");
}
}

2:运行测试方法

 @Test
/**测试组件扫描模式下控制bean的实例化*/
public void testExampleBean1(){
ApplicationContext ac = getApplicationContext();
ExampleBean1 tb1 = ac.getBean("exampleBean1", ExampleBean1.class);
ExampleBean1 tb2 = ac.getBean("exampleBean1",ExampleBean1.class);
System.out.println(tb1 == tb2);
}

Spring学习总结五——SpringIOC容器五

根据结果可以看出默认情况下,是单例模式,虽然调用两次,但是是同一个对象!

3:在类上面添加@Scope(“prototype”)注解,然后在运行测试方法

Spring学习总结五——SpringIOC容器五

可以看出创建了两个对象!

如果在将@Scope修改为singleton时,那么又会是单例模式了。

3:初始化和销毁对象

@postConstruct和@preDestroy两个注解,它们的作用就相当于在配置文件的bean元素中

添加init-method方法和destroy-method方法

 /**
*
*/
package com.hlcui.dao;
import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy; import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component; /**
* @author Administrator
*
*/
@Component
@Scope("prototype")
public class ExampleBean1 { public ExampleBean1() {
System.out.println("实例化ExampleBean1...");
} @PostConstruct
public void init(){
System.out.println("初始化ExampleBean1...");
} @PreDestroy
public void destroy(){
System.out.println("销毁ExampleBean1...");
}
}
相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,104
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,580
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,428
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,200
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,835
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,918