zoukankan      html  css  js  c++  java
  • 注解模式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); 
    	}
    	
    	
    	
    }
    

      

      

  • 相关阅读:
    纯CSS打造可折叠树状菜单
    c++ Constructor FAQ 继续
    Java设计模式偷跑系列(六)Singleton模式的建模与实现
    优秀的产品经理是怎样炼成的?
    Pki原则
    屌丝男初中丰富的工作实践反击
    Android Material Design带UI变化
    unity多边形uv地图
    BZOJ 1208 HNOI2004 宠物收容所 平衡树/set
    [连载]Java程序设计(04)---任务驱动的方法:工资结算系统
  • 原文地址:https://www.cnblogs.com/1020182600HENG/p/6864187.html
Copyright © 2011-2022 走看看