SpringMVC注解--@Component

298 阅读1分钟

@Component

需求:

利用@Component注解把POJO实例化到Spring容器中。

1.新建DemoObj实体类

package com.eleven.domain;

import org.springframework.stereotype.Component;

@Component("demoObj") // 将实体类放到Spring容器中,成为一个Bean
public class DemoObj {
	private Long id;
	private String name = "ddd";

	public DemoObj() {
		super();
	}

	public DemoObj(Long id, String name) {
		super();
		this.id = id;
		this.name = name;
	}

	public Long getId() {
		return id;
	}

	public void setId(Long id) {
		this.id = id;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

}

2.新建applicationContext.xml

<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xmlns:aop="http://www.springframework.org/schema/aop"
	xmlns:context="http://www.springframework.org/schema/context"
	xsi:schemaLocation="  
       http://www.springframework.org/schema/beans  
       http://www.springframework.org/schema/beans/spring-beans-3.0.xsd  
       http://www.springframework.org/schema/aop  
       http://www.springframework.org/schema/aop/spring-aop-3.0.xsd  
       http://www.springframework.org/schema/context  
       http://www.springframework.org/schema/context/spring-context-3.0.xsd">
	<!-- component-scan 会自动开启bean自动注册,base-package会扫描该包下的特殊注解 -->
	<context:component-scan
		base-package="com.eleven.domain" />

</beans>

3.运行

package com.eleven.test;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.eleven.domain.DemoObj;

public class Client {
	public static void main(String[] args) {
		ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml");
		DemoObj demoObj = (DemoObj) ac.getBean("demoObj");
		System.out.println(demoObj.getName());

	}

}

4.输出

ddd

需求:

利用@Component注解和@Autowired注解将POJO注入到Spring容器中。

5.新建DemoAuto实体类

package com.eleven.domain;

import javax.annotation.Resource;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Component("demoAuto")
public class DemoAuto {

	@Autowired	// 将DemoObj注入到DemoAuto里面
	// @Resource(name = "demoObj") // 使用资源指定
	private DemoObj demoObj;

	public DemoObj getDemoObj() {
		return demoObj;
	}

	public void setDemoObj(DemoObj demoObj) {
		this.demoObj = demoObj;
	}

}

6.运行

package com.eleven.test;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.eleven.domain.DemoAuto;
import com.eleven.domain.DemoObj;

public class Client {
	public static void main(String[] args) {
		ApplicationContext ac = new ClassPathXmlApplicationContext("ApplicationContext.xml");
		// DemoObj demoObj = (DemoObj) ac.getBean("demoObj");
		// System.out.println(demoObj.getName());
		DemoAuto demoAuto = (DemoAuto) ac.getBean("demoAuto");
		System.out.println(demoAuto.getDemoObj().getName());

	}

}

7.输出

ddd