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

    在这里插入图片描述


  • 相关阅读:
    RMAN 增量备份 的 对象测试
    小论工具类App的盈利之道
    linux下二进制文件比较程序
    [置顶] 对iOS开发有用的一些自动化处理脚本
    [Win8]Windows8开发笔记(八):数据绑定的基础
    NetBeans 时事通讯(刊号 # 116 Sep 11, 2010)
    域名信息证实 JavaEye 已被 CSDN 收购
    插件架构简介
    GAE for Java exception: no matching index found.
    Java 7 最快要到 2012 年中发布
  • 原文地址:https://www.cnblogs.com/DiZhang/p/12544861.html
Copyright © 2011-2022 走看看