Spring(四)-注解

导入jar包
commons-logging.jar
spring.jar

配置xml文件(开启注解)


 
        
        
        
        

写实体类

package com.fyl.entitiy;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

//Spring约束文件在这里
//C:\Users\lanou3g\Desktop\第三阶段\第7天_Spring\4.Resourse\spring-framework-4.1.4.RELEASE\docs\spring-framework-reference\html
@Component("person")
public class Person {
    
    //注入Student对象
    @Autowired//注意引入包为javax
    //@Resource(name="student")
    private Student stu;

    public Student getStu() {
        return stu;
    }
    
//  public void setStu(@Qualifier("stu") Student stu) {
    public void setStu(Student stu) {
        this.stu = stu;
    }
    
    public void say(){
        System.out.println("hello Person");
    }
}
package com.fyl.entitiy;

import org.springframework.stereotype.Component;

@Component("student")
public class Student {

    public void say(){
        System.out.println("hello Student");
    }
}

测试类

    @Test
    public void testConn(){
        //初始化Spring容器
        ApplicationContext ctx=new ClassPathXmlApplicationContext("applicationContext.xml");//configLocation 配置路径
        //提取注解
        Person p=(Person)ctx.getBean("person");
        p.say();
        p.getStu().say();
        
    }

你可能感兴趣的:(Spring(四)-注解)