spring 注解

 

Bean注解

@Bean向spring窗口注册一个bean实例,name可指定别名,不指定时默认为get后的名字的小写

public class Persion {
    
    @Bean(name="student2")
    public Student getStudent() {
        Student st = new Student();
        st.setName("1000");
        return st;
    }

}

注册后,可直接从容器中获取并使用该bean

public class Main {
    public static void main(String[] args) {
        ConfigurableApplicationContext context = new ClassPathXmlApplicationContext("test7.xml");
//        Persion ps = (Persion)context.getBean("persion");
//        Student st = ps.getStudent();
//        String name = st.getName();
//        System.out.println(name);
    
        Student st2 = (Student)context.getBean("student2");
        String name = st2.getName();
        System.out.println(name);
        context.close();
    }
}

输出

1000

 

@PathVariable注解使用

 

posted @ 2020-11-16 11:30  方诚  阅读(68)  评论(0编辑  收藏  举报