Spring的@Component注解的使用
来源:互联网 发布:淘宝 饰品店 利润 编辑:程序博客网 时间:2024/04/28 10:10
ScanTest.java
package cn.xhx.spring.scan.test;import org.junit.Test;import org.springframework.context.ApplicationContext;import org.springframework.context.support.ClassPathXmlApplicationContext;import cn.xhx.spring.scan.Person;public class ScanTest {/** * 原理 * 1、启动spring容器,sql容器解析配置文件 * 2、当解析到<context:component-scan base-package="cn.xhx.spring.scan"> * </context:component-scan> * 就会在上面指定的包及其子包中扫描所有类,看哪些类上面有@Component注解 * 3、如果有注解,则有如下规则 * @Component * public class PersonDaoImpl { * * } * 等于 * <bean id="personDaoImpl" class="..."></bean> id的值:把类的第一个字母变成小写,其他字母不变 * ---------------------------------- * @Component("personDao") * public class PersonDaoImpl { * * } * 等于 * <bean id="personDao" class="..."></bean> * 4、按照@Resource赋值 */@Testpublic void ScanTest() {ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");Person person = (Person)applicationContext.getBean("person");person.getStudent().say();//结果:"student say"}}
Person.java
package cn.xhx.spring.scan;import javax.annotation.Resource;import org.springframework.stereotype.Component;@Component("person")public class Person {@Resource(name="student")private Student student;public Student getStudent() {return student;}}
Student.java
package cn.xhx.spring.scan;import org.springframework.stereotype.Component;@Component("student")public class Student {public void say() {System.out.println("student say");}}
applicationContext.xml
<?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-2.5.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd"><context:component-scan base-package="cn.xhx.spring.scan"></context:component-scan></beans>
0 0
- Spring的@Component注解的使用
- Spring注解@Component和@resource的使用
- Spring注解@Component和@resource的使用
- Spring注解@Component和@resource的使用
- @Component注解的使用
- Spring @component 注解的作用
- Spring注解的使用和区别:@Component、@Service、@Repository、@Controller
- Spring注解的使用和区别:@Component、@Service、@Repository、@Controller
- spring中@Component @resource注解的说明
- spring的@Component,@Service,@Controller,@Repository注解
- spring的注解(1): @Configuration,@Bean,@Component,
- Spring:使用@Component进行注解
- Spring--IoC--基于注解的DI-@Component相关注解
- @component的注解
- Spring 注解版简单实例之强大的@Component(二)
- Spring中注解@Controller@Service@Repository@Component的区别
- Spring的注解类(@Component、@Repository、@Service、@Controller )
- spring 中四种注解@controller,@service,@repository,@component的区别
- 分析system_call中断处理过程
- 【BZOJ1022】[SHOI2008]小约翰的游戏John【Anti-Nim】
- css3学习笔记-css3选择器
- UIApplicationDelegate分析小结
- select语句的高级应用及实例
- Spring的@Component注解的使用
- JAVA之接口与抽象类详解
- 搭车系统 Server端
- Scala : 面向表达式
- QML--学习第一篇
- JDK源码阅读——Map(HashMap\TreeMap\LinkedHashMap)
- HDOJ 1092 A+B for Input-Output Practice (IV)
- 微信公众号的开发
- 士兵杀敌(一)