Spring

Spring注解Component使用

rzk · 3月31日 · 2020年本文共485个字 · 预计阅读2分钟107次已读
写个实体
//
等价于 在xml配置 @Component public class Dog { private String name; public String getDog() { return name; } @Value("狗子") public void setDog(String dog) { this.name = dog; } public void shout(){ System.out.println("狗叫"); } @Override public String toString() { return "Dog{" + "name='" + name + ''' + '}'; } }
可以通过setName加上value 也可以在字段名上注入value ,在get的话不能
"1.0" encoding="UTF-8"?>
"http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:aop="http://www.springframework.org/schema/aop"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
        https://www.springframework.org/sch睿共享ema/beans/spring-beans.睿共享xsd
        http://www.springframework.or睿共享g/schema/context
        https://www.springframework.org/schema/context/spring-context.xsd
        htt睿共享p://www.springframework.org/schema/aop
        https://www.springframework.org/schema/aop/spring-aop.xsd"
>
    
    base-package="com.rzk.pojo"/>
    
    

    @Test
    public void test(){
        ApplicationCont睿共享ext context = new ClassPathXmlApplicationContext("applocationContext.xml");
        Dog dog = context.getBean("dog", Dog.class);
        System.out.println(dog.getDog());
    }

Spring注解Component使用

0 条回应