博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Spring注解驱动开发(1):容器
阅读量:4059 次
发布时间:2019-05-25

本文共 2235 字,大约阅读时间需要 7 分钟。

核心容器:

Demo:

在这里插入图片描述

pom.xml

4.0.0
com.zhagndi
spring-annotation
0.0.1-SNAPSHOT
org.springframework
spring-context
4.3.12.RELEASE

beans.xml

Person:

package com.zhangdi.bean;public class Person {
private String name; private Integer age; public Person() {
super(); // TODO Auto-generated constructor stub } public Person(String name, Integer age) {
super(); this.name = name; this.age = age; } public String getName() {
return name; } public void setName(String name) {
this.name = name; } public Integer getAge() {
return age; } public void setAge(Integer age) {
this.age = age; } @Override public String toString() {
return "Person [name=" + name + ", age=" + age + "]"; }}

MainConfig.java:

package com.zhangdi.config;import org.springframework.context.annotation.Bean;import org.springframework.context.annotation.Configuration;import com.zhangdi.bean.Person;/** * @author 张 迪 *  */@Configurationpublic class MainConfig {
@Bean("person") public Person person() {
return new Person("lisi",20); }}

MainTest.java:

package com.zhangdi;import org.springframework.context.ApplicationContext;import org.springframework.context.annotation.AnnotationConfigApplicationContext;import org.springframework.context.support.ClassPathXmlApplicationContext;import com.zhangdi.bean.Person;import com.zhangdi.config.MainConfig;public class MainTest {
@SuppressWarnings("resource") public static void main(String[] args) {
// TODO Auto-generated constructor stub// ApplicationContext applicationContext = new ClassPathXmlApplicationContext("beans.xml");// Object bean = applicationContext.getBean("Person");// System.out.println(bean); ApplicationContext applicationContext = new AnnotationConfigApplicationContext(MainConfig.class); Person bean = applicationContext.getBean(Person.class); System.out.println(bean); String[] beanNamesForType = applicationContext.getBeanNamesForType(Person.class); for (String name : beanNamesForType) {
System.out.println(name); } }}

在这里插入图片描述


你可能感兴趣的文章
2020年终总结
查看>>
Homebrew指令集
查看>>
React Native(一):搭建开发环境、出Hello World
查看>>
React Native(二):属性、状态
查看>>
JSX使用总结
查看>>
React Native(四):布局(使用Flexbox)
查看>>
React Native(七):Android双击Back键退出应用
查看>>
Android自定义apk名称、版本号自增
查看>>
adb command not found
查看>>
Xcode 启动页面禁用和显示
查看>>
【剑指offer】q50:树中结点的最近祖先
查看>>
二叉树的非递归遍历
查看>>
【leetcode】Reorder List (python)
查看>>
【leetcode】Linked List Cycle (python)
查看>>
【leetcode】Linked List Cycle (python)
查看>>
【leetcode】Candy(python)
查看>>
【leetcode】Clone Graph(python)
查看>>
【leetcode】Sum Root to leaf Numbers
查看>>
【leetcode】Pascal's Triangle II (python)
查看>>
java自定义容器排序的两种方法
查看>>