spring 注解方式

1.项目清单


2.项目源码

   ① bean

package com.bean;

public class Student {
private String sname;
private int sid;
public String getSname() {
	return sname;
}
public void setSname(String sname) {
	this.sname = sname;
}
public int getSid() {
	return sid;
}
public void setSid(int sid) {
	this.sid = sid;
}
public Student() {
}
}
package com.bean;

import javax.annotation.Resource;

import org.springframework.stereotype.Component;

@Component("StudentService")
public class StudentService {
private Student student;
public StudentService() {
}
public Student getStudent() {
	return student;
}
@Resource
public void setStudent(Student student) {
	this.student = student;
}
}
package com.bean;

public class User {
private String name;
private int age;
public String getName() {
	return name;
}
public void setName(String name) {
	this.name = name;
}
public int getAge() {
	return age;
}
public void setAge(int age) {
	this.age = age;
}
@SuppressWarnings("unused")
private void init() {
	System.out.println("User init");
}
@SuppressWarnings("unused")
private void destroy() {
	System.out.println("User destroy");
}
public User() {
	// TODO Auto-generated constructor stub
}
public User(String name, int age) {
	this.name = name;
	this.age = age;
}
}
package com.bean;

import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;
import javax.annotation.Resource;
import javax.annotation.Resources;

import org.springframework.stereotype.Component;
@Component("User02")
public class User02 {
private String name;
private int age;
public String getName() {
	return name;
}
public void setName(String name) {
	this.name = name;
}
public int getAge() {
	return age;
}
public void setAge(int age) {
	this.age = age;
}
/**
 * @PostConstruct 初始化
 * @PreDestroy 销毁
 */
@PostConstruct
private void init() {
	System.out.println("User02 init");
}
@PreDestroy
private void destroy() {
	System.out.println("User02 destroy");
}
public User02() {
	// TODO Auto-generated constructor stub
}
public User02(String name, int age) {
	this.name = name;
	this.age = age;
}
}
package com.bean;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.annotation.Required;

public class UserImplService {
private User User01;

private String userBean;

public User getUser01() {
	return User01;
}
/**
 *  @Qualifier 自动装配,当有两个User的类型对象,那么它装配User01;
 *  @Autowired(required=false) 不是必须注入的;
 */
@Autowired
public void setUser01(@Qualifier(value="User01") User user01) {
	User01 = user01;
}
public String getUserBean() {
	return userBean;
}
/**
 * @Required 必须注入值才能行;
 */
@Required
public void setUserBean(String userBean) {
	this.userBean = userBean;
}

public UserImplService(User user01, String userBean) {
	User01 = user01;
	this.userBean = userBean;
}
public UserImplService() {
}
}
package com.bean;

import javax.annotation.Resource;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.annotation.Required;

public class UserImplService01 {
private User User01;

private String userBean;

public User getUser01() {
	return User01;
}
/**
 * @Resource(name="user") byName注入;
 */
@Resource(type=User.class)
public void setUser01(@Qualifier(value="User01") User user01) {
	User01 = user01;
}
public String getUserBean() {
	return userBean;
}
public void setUserBean(String userBean) {
	this.userBean = userBean;
}

public UserImplService01(User user01, String userBean) {
	User01 = user01;
	this.userBean = userBean;
}
public UserImplService01() {
}
}

   ② 测试

package com.test;

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

import com.bean.User;
import com.bean.UserImplService;

public class Test01 {
public static void main(String[] args) {
	ClassPathXmlApplicationContext act = new ClassPathXmlApplicationContext("applicationContext.xml");
	System.out.println(act);
	User user = (User)act.getBean("User");
	System.out.println(user.getAge());
	UserImplService us = (UserImplService)act.getBean("us");
	System.out.println(us.getUser01().getAge());
	act.destroy();
	
}
}
package com.test;

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

import com.bean.Student;
import com.bean.StudentService;

public class TestForResource {
public static void main(String[] args) {
	ApplicationContext act = new ClassPathXmlApplicationContext("applicationContext.xml");
	StudentService ss = (StudentService) act.getBean("StudentService");
	Student s = ss.getStudent();
	System.out.println(s.getSname());
}
}
package com.test;

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

import com.bean.User;
import com.bean.User02;
import com.bean.UserImplService;
import com.bean.UserImplService01;

public class TestForUser02 {
public static void main(String[] args) {
	ClassPathXmlApplicationContext act = new ClassPathXmlApplicationContext("applicationContext.xml");
	User02 user = (User02)act.getBean("User02");
	System.out.println(user.getName());
	act.destroy();
}
}

   ③ application.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:context="http://www.springframework.org/schema/context"
	xmlns:p="http://www.springframework.org/schema/p"
	xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
	http://www.springframework.org/schema/context
           http://www.springframework.org/schema/context/spring-context-2.5.xsd">

	<context:annotation-config />
	<context:component-scan base-package="com" />

	<bean class="com.bean.User" name="User" init-method="init"
		destroy-method="destroy">
		<property name="age" value="26"></property>
		<property name="name" value="ZH"></property>
	</bean>

	<bean class="com.bean.User" init-method="init" destroy-method="destroy">
		<!-- qualifier 跟name的功能是一样的 -->
		<qualifier value="User01"></qualifier>
		<property name="age" value="24" />
		<property name="name" value="US" />
	</bean>
	<!--
		<bean class="com.bean.UserImplService" id="us" autowire="byType">
	-->
	<bean class="com.bean.UserImplService" id="us">
		<property name="userBean" value="UserBean"></property>
		<!--
			<property name="user" ref="User"></property> <property
			name="userBean" value="UserBean"></property>
		-->
	</bean>
	<bean class="com.bean.UserImplService01" id="u01">
		<property name="userBean" value="UserBean02"></property>
		<!--
			<property name="user" ref="User"></property> <property
			name="userBean" value="UserBean"></property>
		-->
	</bean>
<bean class="com.bean.Student" name="Student">
<property name="sid" value="1"></property>
<property name="sname" value="marry"></property>
</bean>
</beans>


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值