注解模式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);
	}

}

  

  

时间: 2024-08-07 14:49:20

注解模式1的相关文章

spring的配置模式与注解模式基础

“依赖注入”是spring的核心特征,在Web服务器(如Tomcat)加载时,它会根据Spring的配置文件中配置的bean或者是通过注解模式而扫描并装载的bean实例自动注入到ApplicationContext容器中(ApplicationContext容器管理了被注入的bean对象). 下面做两个简单测试以说明spring“依赖注入“的两种模式:配置模式与注解模式. 测试工具: 一.新建spring配置文件,放在类目录下 在“src”上右键点"new",选择"Other

JAVA-SpringMVC基于注解模式第一个应用

项目文件结构 1. web.xml配置文件 <?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun

Mybatis(dao层)基于注解模式的配置方式

1.还是要新建Shop.xml文件 <?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd"> <mapper namespace="com.dao.Sh

Spring MVC 4.1.3 + MyBatis 零基础搭建Web开发框架(注解模式哦)

首先感谢一下润和软件,指引我走上了Spring MVC Web开发的道路. 下面进入正题 搭建开发环境: Netbeans8.0.2 + MySql5.6 + JDK1.7 + tomcat8.0.15 本次采用的Spring MVC Jar包如下: spring-aop-4.1.3.RELEASE.jar spring-beans-4.1.3.RELEASE.jar spring-context-4.1.3.RELEASE.jar spring-context-support-4.1.3.RE

SpringBoot2.0 基础案例(13):基于Cache注解模式,管理Redis缓存

本文源码 GitHub地址:知了一笑 https://github.com/cicadasmile/spring-boot-base 一.Cache缓存简介 从Spring3开始定义Cache和CacheManager接口来统一不同的缓存技术:Cache接口为缓存的组件规范定义,包含缓存的各种操作集合:Cache接口下Spring提供了各种缓存的实现:如RedisCache,EhCacheCache ,ConcurrentMapCache等: 二.核心API 1.Cache缓存接口定义缓存操作.

注解模式2

IDao.java package com.wh.IDao; public interface IDao { boolean insert(); } OracleDaoImpl.java package com.wh.dao; import org.springframework.stereotype.Repository; import com.wh.IDao.IDao; @Repository public class OracleDaoImpl implements IDao{ @Over

springmvc常见注解模式

1 常用注解元素 2 @Controller 3 标注在Bean的类定义处 4 @RequestMapping 5 真正让Bean具备 Spring MVC Controller 功能的是 @RequestMapping 这个注解 6 @RequestMapping 可以标注在类定义处,将 Controller 和特定请求关联起来: 7 还可以标注在方法签名处,以便进一步对请求进行分流 8 9 配套的属性有: 10 value 需要跳转的地址 11 method 基于RestFul的跳转参数,有

Struts2 注解模式

相信大家一定看到了两个class中定义了一样的action,不过看类的元数据,是不同的命名空间.这里比较重要(对我来说)的是 @Action(value = "/login", results = { @Result(name = "sucess", location = "/index.jsp"), @Result(name = "chain", location = "chain", type = &q

SSH(Struts2+Spring+Hibernate)框架搭建流程&lt;注解的方式创建Bean&gt;

此篇讲的是MyEclipse9工具提供的支持搭建自加包有代码也是相同:用户登录与注册的例子,表字段只有name,password. SSH,xml方式搭建文章链接地址:http://www.cnblogs.com/wkrbky/p/5912810.html 一.Hibernate(数据层)的搭建: 实现流程 二.Spring(注入实例)的使用: 实现流程 三.Struts2(MVC)的搭建: 实现流程 这里注意一点问题: Struts2与Hibernate在一起搭建,antlr包,有冲突.MyE