注解模式1

applicationContext.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" xmlns:mvc="http://www.springframework.org/schema/mvc"
	xmlns:aop="http://www.springframework.org/schema/aop" xmlns:context="http://www.springframework.org/schema/context" xmlns:c="http://www.springframework.org/schema/c"
	xmlns:cache="http://www.springframework.org/schema/cache"
	xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd
		http://www.springframework.org/schema/cache http://www.springframework.org/schema/cache/spring-cache-4.0.xsd
		http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
		http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd
		http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd">
	
	<!-- <context:component-scan base-package="com.wh.*"></context:component-scan>  -->
	<context:component-scan base-package="com.wh"></context:component-scan>

</beans>

User.java

package com.wh.po;

import javax.annotation.Resource;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Lazy;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

import com.wh.bean.Hobby;

@Component(value="user")
@Lazy(true)
@Scope("singleton")
public class User {
	@Value(value="1101")
	private Integer id;
	private String username;
	private String password;
	
	@Resource(name="hobby")
	private Hobby hobby;
	
	public User() {
		// TODO Auto-generated constructor stub
		System.out.println("User.class无参构造方法");
	} 

	public User(Integer id, String username, String password) {
		super();
		this.id = id;
		this.username = username;
		this.password = password;
	}

	public Integer getId() {
		return id;
	}

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

	public String getUsername() {
		return username;
	}

	public void setUsername(String username) {
		this.username = username;
	}

	public String getPassword() {
		return password;
	}

	public void setPassword(String password) {
		this.password = password;
	}

	@Override
	public String toString() {
		return "User [id=" + id + ", username=" + username + ", password=" + password + "]";
	}

}

Test.java

package com.wh.test;

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

import com.wh.bean.Student;
import com.wh.po.User;

public class TestMVC {

	@Test
	public void testUser(){
		ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml");
		//User user =(User)ac.getBean("user");
		//System.out.println(user); 
	}
	
	
	
}

  

  

posted @ 2017-05-16 22:54  奋斗的少年WH  阅读(105)  评论(0编辑  收藏  举报