首页 技术 正文
技术 2022年11月14日
0 收藏 790 点赞 3,828 浏览 5761 个字

配置文件:

 <?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd"> <!--context:component-scan 指定 扫描的包 -->
<!--可以通过 resource-pattern 指定扫描的资源, resource-pattern="myrepository/*.class"
的含义: 只扫描 base-package 对应包下的 目录为 myrepository 的所有java Bean -->
<!-- <context:component-scan base-package="imooc_spring.test.anotation"
resource-pattern="myrepository/*.class"></context:component-scan> --> <!--
context:exclude-filter type="annotation"
expression="org.springframework.stereotype.Repository"
子节点指定排除哪些注解
context:include-filter type="annotation" 需要结合context:component-scan 标签的
use-default-filters="false"来使用 context:exclude-filter type="assignable"
这个expression指的是自己写的类,意思排除哪些类
expression="imooc_spring.test.anotation.TestObj"
-->
<context:component-scan base-package="imooc_spring.test.anotation" >
<!-- <context:exclude-filter type="annotation"
expression="org.springframework.stereotype.Repository" /> --> <context:exclude-filter type="assignable"
expression="imooc_spring.test.anotation.TestObj" />
</context:component-scan>
</beans>

@Repository注解:

 package imooc_spring.test.anotation.myrepository; import org.springframework.stereotype.Repository; /**
* 指定id,默认为dAO,即类名首字母小写,如果指定了名称那么只能ctx.getBean(指定名称)来获取bean
* 这个例子里就只能通过ctx.getBean("wyldao)来获取DAO 的实例了;
*
* @author Wei
*/
@Repository("wyldao")
public class DAO {
/**
* 返回x和y的乘积
*
* @param x
* @param y
* @return x*y
*/
public int multi(int x, int y) {
return x * y;
}
}

@Component 注解:

 package imooc_spring.test.anotation; import org.springframework.stereotype.Component;
/**
* Component 注解
* @author Wei
*
*/
@Component
public class TestObj {
public void SayHi(){
System.out.println("\nHi this is TestObj.SayHi()...");
}
}

@Controller注解:

 package imooc_spring.test.anotation; import org.springframework.stereotype.Controller; @Controller
public class UserController {
public void execute(){
System.out.println("\nUserController.execute()...");
}
}

@Repository注解:

 package imooc_spring.test.anotation; import org.springframework.stereotype.Repository; //@Repository
@Repository("wyl_repo")
public class UserRepositoryImpl implements IUserRepository {
//模拟持久化层
@Override
public void save() {
// TODO Auto-generated method stub
System.out.println("\nUserRepositoryImpl.save()...");
} }

@Service注解:

 package imooc_spring.test.anotation; import org.springframework.stereotype.Service; @Service
public class UserService {
public void add(){
System.out.println("\nUserService.add()...");
}
}

2.另外一组注解:

@Autowired、@Resource 这两个注解的功能可以说是一样的,前面一组注解的功能也是类似的。

@Autowired 注解自动装配具有兼容类型的单个 Bean属性

可以用来标识

1 构造器,

2 普通字段(即使是非 public),

3 一切具有参数的方法

这三种都可以应用@Authwired 注解

默认情况下, 所有使用 @Authwired 注解的属性都需要被设置. 当 Spring 找不到匹配的 Bean 装配属性时, 会抛出异常, 若某一属性允许不被设置, 可以设置 @Authwired 注解的 required 属性为 false

@Resource 和 @Inject 注解,这两个注解和 @Autowired 注解的功用类似

@Resource 注解要求提供一个 Bean 名称的属性,若该属性为空,则自动采用标注处的变量或方法名作为 Bean 的名称

@Inject 和 @Autowired 注解一样也是按类型匹配注入的 Bean, 但没有 reqired 属性

建议使用 @Autowired 注解

 package imooc_spring.test.anotation; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service; @Service
public class UserService {
@Autowired(required=false)
public UserRepositoryImpl rep ; public void add(){
System.out.println("\nUserService.add()...");
}
}
 package imooc_spring.test.anotation; import org.springframework.stereotype.Repository; //@Repository
@Repository("wyl_repo")
public class UserRepositoryImpl implements IUserRepository {
//模拟持久化层
@Override
public void save() {
// TODO Auto-generated method stub
System.out.println("\nUserRepositoryImpl.save()...");
} }

@Autowired(required=false)

这个就相当于IOC容器里的ref属性,比如

 <bean id="people" class="test.spring.autowired.Person" scope="prototype"
autowire="byName">
<property name="name" value="小明"></property>
<property name="cat" ref="cat222"></property>
</bean>
<bean id="cat222" class="test.spring.autowired.Cat">
<property name="name" value="我是小喵喵"></property>
</bean>

整个IOC容器:

 <?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd">
<bean id="moocAppctx" class="imooc_spring.test.aware.MoocApplicationContext"
init-method="hhhh">
</bean> <!--context:component-scan 指定 扫描的包 -->
<!--可以通过 resource-pattern 指定扫描的资源, resource-pattern="myrepository/*.class"
的含义: 只扫描 base-package 对应包下的 目录为 myrepository 的所有java Bean -->
<!-- <context:component-scan base-package="imooc_spring.test.anotation"
resource-pattern="myrepository/*.class"></context:component-scan> --> <!-- context:exclude-filter type="annotation" expression="org.springframework.stereotype.Repository"
子节点指定排除哪些注解 context:include-filter type="annotation" 需要结合context:component-scan
标签的 use-default-filters="false"来使用 context:exclude-filter type="assignable"
这个expression指的是自己写的类,意思排除哪些类 expression="imooc_spring.test.anotation.TestObj" -->
<context:component-scan base-package="imooc_spring.test.anotation">
<!-- <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Repository"
/> --> <!-- <context:exclude-filter type="assignable" expression="imooc_spring.test.anotation.TestObj"
/> --> </context:component-scan> </beans>

中的ref,一个bean引用另一个bean,上面的例子中就是

UserService 这个bean引用 UserRepositoryImpl 这个bean,

当然了,使用这些注解的时候都需要结合

<context:component-scan base-package="imooc_spring.test.anotation">
</context:component-scan>

来一起使用。

@Autowired(required=false)

中的required属性可以不写,不写的时候默认为required=true,表示如果没有找到要引用的bean,那么引用的这个bean就为null,

但是这样子的话可能会由于引用的bean属性为null,从而可能会引起空指针异常。

Spring注解配置

相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,492
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,907
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,740
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,495
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:8,132
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:5,297