java旅行--第七站--Spring的注解

本文介绍了Spring框架的作用及其依赖注入的方式,详细解释了set注入的实现,并通过具体代码示例展示了如何配置Bean来实现依赖注入。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Spring的作用:解决类与类之间的强耦合问题。

有三种注入方式,构造方法注入,接口注入,还有set注入,其中set注入最常用,set注入代码如下

User类与email类代码如下

public class User {
	private String name;
	private Email email;
	private List<String> hobby;
	
	public List<String> getHobby() {
		return hobby;
	}

	public void setHobby(List<String> hobby) {
		this.hobby = hobby;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public Email getEmail() {
		return email;
	}

	public void setEmail(Email email) {
		this.email = email;
	}
}

public class Email {
	private String emailName;

	public String getEmailName() {
		return emailName;
	}

	public void setEmailName(String emailName) {
		this.emailName = emailName;
	}
	
}

配置文件代码如下

	<bean id="user" class="spring.User" scope="prototype">
		<property name="name">
			<value>freshbin</value>
		</property>
		<property name="email">
			<ref bean="emailid"/>
		</property>
		<property name="hobby">
			<list>
				<value>打代码</value>
				<value>下棋</value>
			</list>
		</property>
	</bean>
	<bean id="emailid" class="spring.Email">
		<property name="emailName">
			<value>666666@qq.com</value>
		</property>
	</bean>

在使用spring时,必须建立一个application.xml配置文件,其完整内容如下

<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xmlns="http://www.springframework.org/schema/beans" xmlns:aop="http://www.springframework.org/schema/aop"
	xmlns:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx"
	xmlns:cache="http://www.springframework.org/schema/cache" xmlns:p="http://www.springframework.org/schema/p"
	xsi:schemaLocation="http://www.springframework.org/schema/beans 
     http://www.springframework.org/schema/beans/spring-beans-4.2.xsd
     http://www.springframework.org/schema/aop
     http://www.springframework.org/schema/aop/spring-aop-4.2.xsd
     http://www.springframework.org/schema/context
     http://www.springframework.org/schema/context/spring-context-4.2.xsd">
	
	<context:component-scan base-package="user"></context:component-scan>

	<bean id="user" class="spring.User" scope="prototype">
		<property name="name">
			<value>freshbin</value>
		</property>
		<property name="email">
			<ref bean="emailid"/>
		</property>
		<property name="hobby">
			<list>
				<value>打代码</value>
				<value>下棋</value>
			</list>
		</property>
	</bean>
	<bean id="emailid" class="spring.Email">
		<property name="emailName">
			<value>666666@qq.com</value>
		</property>
	</bean>
</beans>
其中ref是指定注入另一个对象,属性则是用property标签,有name与value注入,list,set,map集合也是同理。

<context:component-scan base-package="user"></context:component-scan>这行代码是让spring对user下面的所有包进行扫描,不然程序运行时,spring注入没法实现

spring的注解如下

@Autowired按类型注解
@Qualifier按ID注解
@Controller控制层的注解
@Service业务逻辑层的注解
@Repository DAO层的注解
@Scope控制多实例还是单实例的注解,在Web应用还有request和session
@Component注解可以放在类的头上,@Component不推荐使用。
其中@Autowired是当一个类需要引入其他类时,加入的注解,如下

@Service("userServiceImpl")
@Scope("prototype")
public class UserServiceImpl implements UserService {
	
	@Autowired
	private UserDao userDao;
	@Override
	public boolean checkLogin(String account, String pwd) {
		userDao.CheckAccount();
		System.out.println("UserServiceImpl.checkLogin");
		return false;
	}

}
当一个接口有多个实现类时,则加入@Qualifier("userServiceImpl")代表指定的实现类





评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值