Component 用法

给参数赋值的一个操作,代码如下

1 @Component
2 public class User {
3     //相当于:<property name="name" value="嘻嘻哈哈嘻嘻"/>
4     @Value("嘻嘻哈哈嘻嘻")
5     public String name;
6 
7 }

使用时,要在配置文件里扫描到这个类的包,才能操作

<context:component-scan base-package="com.ma.pojo"/>

最后还要加上这样一句话

<context:annotation-config/>

总的配置文件如下:

 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <beans xmlns="http://www.springframework.org/schema/beans"
 3        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 4        xmlns:context="http://www.springframework.org/schema/context"
 5        xmlns:aop="http://www.springframework.org/schema/aop"
 6        xsi:schemaLocation="http://www.springframework.org/schema/beans
 7        http://www.springframework.org/schema/beans/spring-beans.xsd
 8        http://www.springframework.org/schema/context
 9        http://www.springframework.org/schema/context/spring-context.xsd
10        http://www.springframework.org/schema/aop
11        http://www.springframework.org/schema/aop/spring-aop.xsd">
12     <context:component-scan base-package="com.ma.pojo"/>
13 
14     <context:annotation-config/>
15 </beans>

对应于项目中的Spring-06-anno

posted @ 2021-04-15 13:19  古比  阅读(102)  评论(0编辑  收藏  举报