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

    在这里插入图片描述


  • 相关阅读:
    关于Class.forName(“com.mysql.jdbc.Driver”)
    Vector既然继承了AbstractList为啥还要实现List接口
    推荐两个支持Java的云主机空间
    关于“Return empty arrays or collections, not nulls”的思考
    "win7回收站已损坏"解决方法
    ibatis中使用like模糊查询
    当你升级到ubuntu12.04之后
    转一篇:如何快速的修改参考文献
    Java Annotations初探
    用eclipse开发android,xmllayout文件不自动提示,Java代码可以自动提示
  • 原文地址:https://www.cnblogs.com/DiZhang/p/12544861.html
Copyright © 2011-2022 走看看