Spring注解驱动开发(1):容器

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/ShelleyLittlehero/article/details/83351487

核心容器:

Demo:

在这里插入图片描述

pom.xml

<project xmlns="http://maven.apache.org/POM/4.0.0"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
	<modelVersion>4.0.0</modelVersion>
	<groupId>com.zhagndi</groupId>
	<artifactId>spring-annotation</artifactId>
	<version>0.0.1-SNAPSHOT</version>

	<dependencies>
		<!-- https://mvnrepository.com/artifact/org.springframework/spring-context -->
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-context</artifactId>
			<version>4.3.12.RELEASE</version>
		</dependency>


	</dependencies>
</project>

beans.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"
	xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
	<!-- <bean></bean> -->
	<bean id="Person" class="com.zhangdi.bean.Person">
		<property name="age" value="11"></property>
		<property name="name" value="zhangsan"></property>
	</bean>
</beans>

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 张 迪
 * 
 */
@Configuration
public 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);
		}
		
	}

}

在这里插入图片描述


猜你喜欢

转载自blog.csdn.net/ShelleyLittlehero/article/details/83351487