zoukankan      html  css  js  c++  java
  • Spring注解驱动开发(1):容器

    核心容器:

    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);
    		}
    		
    	}
    
    }
    
    

    在这里插入图片描述


  • 相关阅读:
    排序系列 之 希尔排序算法 —— Java实现
    排序系列 之 快速排序算法 —— Java实现
    排序系列 之 冒泡排序及其改进算法 —— Java实现
    排序系列 之 折半插入排序算法 —— Java实现
    排序系列 之 直接插入排序算法 —— Java实现
    Three.js入门篇(一)创建一个场景
    THREE.JS(如何想场景中添加物体对象)
    Tween.js 动画效果
    js柯里化
    node path模块
  • 原文地址:https://www.cnblogs.com/DiZhang/p/12544861.html
Copyright © 2011-2022 走看看