spring简单入门-注解
项目目录
建立对应的包,导入需要的jar包
ICustomerDao.java
package com.zh.dao;
public interface ICustomerDao {
void saveCustomer();
}
ICustomerDaoImpl.java
package com.zh.dao.impl;
import org.springframework.stereotype.Repository;
import com.zh.dao.ICustomerDao;
@Repository("iCustomerDaoImpl")
public class ICustomerDaoImpl implements ICustomerDao {
@Override
public void saveCustomer() {
System.out.println("持久层保存了客户");
}
}
ICustomerService.java
package com.zh.service;
public interface ICustomerService {
/**
* 保存客户
*/
void saveCustomer();
}
CustomerServiceImpl.java
package com.zh.service.impl;
import javax.annotation.Resource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;
import com.zh.dao.ICustomerDao;
import com.zh.service.ICustomerService;
/**
* 客户的业务层实现类
* @author zh
*xml方式<bean id="customerService" class="zh.service.impl.CustomerServiceImpl"></bean>
*1.用于创建对象
* @Component 作用:相当于用了一个bean标签。可以出现在类、接口、枚举上定义。属性value,不写时,默认是类名首字母小写
* 由这个注解衍生了其他三个注解,它们的作用及属性都是一模一样
* @Controller 一般用于表现层的注解
* @Service 一般用于业务层的注解
* @Repository 一般用于持久层的注解
*2.用于注入数据的:
*@Autowired自动按照类型注入。只要有唯一的类型匹配就能注入成功
* 如果注入的bean在容器中不唯一的时候,它会把变量名称作为bean的id(ICustomerDao customerDao = null中的customerDao),
* 在容器中查找,找到后也能注入成功,如果没有就会报错,使用这个注解后,set方法就不是必须的
* (public void setICustomerDao(ICustomerDao customerDao){this.customerDao=customerDao})
*
*@Qualifier("iCustomerDaoImpl")在自动按照类型注入的基础上,在按照bean的id注入。它在给类成员注入数据时,不能独立使用。
* 但是在给方法的形参注入数据时,可以独立使用
*
*@Resuorce 直接通过bean的id进行注入
* 通过name进行指定id(必须写name)
* 以上的注解都是用于注入其他bean类型的。用于注入基本类型和String类型,需要用@Value注解方式
* @Value 用于注入基本类型和String类型,可以借助spring的el表达式读取properties文件中的配置
* @Scope 用于改变bean的作用范围,value:singleton prototype request session globalsession
*/
@Service
@Scope("prototype")
public class CustomerServiceImpl implements ICustomerService{
@Value("刘德华")
private String name;
// @Autowired
// @Qualifier("iCustomerDaoImpl")
@Resource(name="iCustomerDaoImpl")
private ICustomerDao customerDao = null;
@Override
public void saveCustomer() {
System.out.println("业务层调用持久层"+"------"+name);
customerDao.saveCustomer();
}
}
test.java
package com.zh.test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.zh.entity.User;
import com.zh.service.impl.CustomerServiceImpl;
/**
* spring入门
* @author zh
*
*/
public class test {
public static void main(String[] args) {
/**
* ClassPathXmlApplicationContex :它只能加载类路径下的配置文件(一般使用这个)
* FileSystemXmlApplicationContext :它可以加载磁盘任意路径下的配置文件
*/
ApplicationContext ac = new ClassPathXmlApplicationContext("classpath:beans.xml");
CustomerServiceImpl customerServiceImpl = ac.getBean("customerServiceImpl", CustomerServiceImpl.class);
customerServiceImpl.saveCustomer();
}
}
beans.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"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd">
<!-- 要告知spring在创建容器时,要扫描的包。当配置此标签后,
spring创建容器就会去指定的包下找对应的注解 标签是在一个context的名称空间里,
所以必须要导入context的名称空间 -->
<context:component-scan base-package="com.zh"></context:component-scan>
</beans>
执行结果