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

      

      

  • 相关阅读:
    java Concurrent包学习笔记(二):CountDownLatch和CyclicBarrier
    java Concurrent包学习笔记(四):BlockingQueue
    Linux Linux程序练习十五(进程间的通信共享内存版)
    Linux shell中的符号
    Linux shell程序一
    Linux Linux程序练习十四(多进程压力测试)
    Linux Linux程序练习十三(信号阻塞,捕获)
    Linux 网络编程详解二(socket创建流程、多进程版)
    Linux 网络编程详解一(IP套接字结构体、网络字节序,地址转换函数)
    Linux shell实战(ipcs工具)
  • 原文地址:https://www.cnblogs.com/1020182600HENG/p/6864187.html
Copyright © 2011-2022 走看看