博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
spring框架学习笔记(十)
阅读量:4456 次
发布时间:2019-06-08

本文共 2471 字,大约阅读时间需要 8 分钟。

通过注解的方式配置bean

 Spring能够从classpath下自动扫描,侦测和实例化具有特定注解的组件。

即,想要实现注解的方式配置bean需要满足2个条件:

  1. 类上面标注特定组件,如:@Component、@Service、@Repository、@Controller
  2. 在配置文件中增加bean-scan:<context:component-scan base-package="com.pfSoft.annotation"></context:component-scan>

以下实例说明:新增People类,请注意命名空间(之前举例的时候都基本都略去了命名空间部分,但是通过bean-scan来实现基于注解的注入时,对于命名空间就比较重要了)

package com.pfSoft.annotation;import org.springframework.stereotype.Component;@Componentpublic class People {	private String name;	private Integer age;	/**	 * 	 * @return the name	 */	public String getName() {		return name;	}	/**	 * @param name the name to set	 */	public void setName(String name) {		this.name = name;	}	/**	 * 	 * @return the age	 */	public Integer getAge() {		return age;	}	/**	 * @param age the age to set	 */	public void setAge(Integer age) {		this.age = age;	}	/* (non-Javadoc)	 * @see java.lang.Object#toString()	 */	@Override	public String toString() {		return "People [name=" + name + ", age=" + age + "]";	}		}

 

 新增接口和实现类:

package com.pfSoft.annotation.service;public interface IPeopleManService {	public Boolean Adult(Integer age);}

 

package com.pfSoft.annotation.service;import org.springframework.stereotype.Service;@Service(value="PeopleManService")public class PeopleManServiceImp implements IPeopleManService {	public Boolean Adult(Integer age) {		Boolean ret=false;		if(age>=18)		{			ret=true;		}		return ret;	}}

 测试方法:

public class testAnnotation {		ApplicationContext ctx=null;	@Before	public void init() {		ctx=new  ClassPathXmlApplicationContext("spring-annotation.xml");	}		@Test	public void test() {		People people= (People) ctx.getBean("people");		people.setAge(8);		people.setName("pf");		System.out.println(people);		IPeopleManService service=(IPeopleManService) ctx.getBean("PeopleManService");		if(service.Adult(people.getAge()))		{			System.out.println(MessageFormat.format("{0}成年了,{1}岁", people.getName(),people.getAge())); 		}		else {			System.out.println(MessageFormat.format("{0}未成年,只有{1}岁", people.getName(),people.getAge()));		}	}}

 输出结果如下:

People [name=pf, age=8]pf未成年,只有8岁

 

配置文件中的过滤

 resource-pattern实现过滤

<context:exclude-filter type="annotation" expression=""/>子节点表示要排除在外的目标类

<context:include-filter type="annotation" expression=""/>表示要包含的目标类

 

通过@Autowired自动装配bean

  • 默认情况下,所有使用@Autowired注解的属性都需要被设置,当Spring找不到匹配的Bean属性是,会跑出异常,若某一属性允许不被设置,可以设置@Autowired注解的required属性为false
  • 如果存在多个类型相同的bean时,可以使用@Qualifier 来确定使用哪个,或者在申明bean的注解上直接加上名字,保证属性名或者字段名一致。

 

转载于:https://www.cnblogs.com/falcon-fei/p/5456779.html

你可能感兴趣的文章
对Netflix Ribbon的Loadbalancer类源码设计合理性的一点质疑
查看>>
关于日历的算法
查看>>
[QT编程]QT实现的一个渐隐渐显窗体
查看>>
在Web工程中引入Jquery插件报错解决方案
查看>>
大学总结之影响我最深的十本书
查看>>
用myEclipse连接数据源生成动态数据报表
查看>>
[myeclipse]@override报错问题
查看>>
자주 쓰이는 정규표현식
查看>>
超简单的listview单选模式SingleMode(自定义listview item)
查看>>
vue-11-路由嵌套-参数传递-路由高亮
查看>>
HDU 1199 - Color the Ball 离散化
查看>>
[SCOI2005]骑士精神
查看>>
Hibernate原理解析-Hibernate中实体的状态
查看>>
六时车主 App 隐私政策
查看>>
C语言常见问题 如何用Visual Studio编写C语言程序测试
查看>>
Web用户的身份验证及WebApi权限验证流程的设计和实现
查看>>
hdu 2098 分拆素数和
查看>>
[ONTAK2010]Peaks kruskal重构树,主席树
查看>>
ECMAScript6-let与const命令详解
查看>>
iOS 使用系统相机、相册显示中文
查看>>