需要被IOC容器接管的类,我们就在他上方增加一个注解@Component
扫描包:看包里面的类是否有加上注解,如果有则创建Bean到IOC容器去
一,为什么要用Spring注解实现扫描包?
Spring注解实现扫描包(通常是通过@ComponentScan注解)是一个非常重要的机制,它使得Spring能够自动发现并注册bean,从而简化Spring应用的配置和开发过程。以下是使用Spring注解实现扫描包的一些主要原因:
简化配置:传统的Spring配置通常涉及大量的XML文件,用于定义bean及其依赖关系。使用注解扫描,这些配置可以大大减少或完全消除,因为Spring可以自动扫描指定包中的类,并根据类上的注解(如@Component, @Service, @Repository, @Controller等)来创建和注册bean。
自动装配:除了创建bean之外,Spring还可以根据类上的注解(如@Autowired, @Resource等)自动装配bean之间的依赖关系。这使得开发者无需在XML配置文件中显式地定义每个bean的依赖关
二,实现步骤
(1)在配置文件加配置包的扫描路径,这个是xml文件
<context:component-scan base-package="com.cykj"></context:component-scan>
<?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 https://www.springframework.org/schema/context/spring-context.xsd"><!-- <!–配置alluser–>-->
<!-- 配置包扫描路径-->
<context:component-scan base-package="com.cykj"></context:component-scan>
</beans>
(2)在需要的类上面添加上注解@Commponent
(3)getBean可以通过名字来获取,名字默认值就是类名改成小驼峰,也可以指定名字
@Commponent(value=" ")
三,常用的注解
(1)@Commponet ------>最大的注解
整个注解无论实在service包还是dao层那边都是可以使用的
(2)@service ------> service层的注解
(3)@Repository ------->Dao层
(4)@value ------>注入的是基本数据类型的属性
(5)@Autowried ------>注入的属性是引用数据类型
四,代码示例
这边简单弄一个登录获取账号密码的
工具类
//首先配置一个IOCUtils类,来写IOC的工具类的
package com.cykj.util;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class IOCUtils {
/**
* 单例函数
* 1.构造函数私有化
* 2.内部提供一个静态的对象,来继承new出来的对象
* 3.对外提供一个public的方法,来访问静态的对象
*/
private static IOCUtils obj;
private static ClassPathXmlApplicationContext cps;
private IOCUtils(){
cps = new ClassPathXmlApplicationContext("spring-ioc.xml");
}
public synchronized static IOCUtils getInstance(){
if (obj == null){
obj = new IOCUtils();
}
return obj;
}
public ClassPathXmlApplicationContext getCps(){
return cps;
}
}
servlet层
//servlet层的
package com.cykj.servlet;
import com.cykj.pojo.Alluser;
import com.cykj.service.impl.UserServiceImpl;
import com.cykj.util.IOCUtils;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
@WebServlet("/user")
public class UserServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
// 接收值
String acc = req.getParameter("acc");
String pwd = req.getParameter("pwd");
UserServiceImpl us = IOCUtils.getInstance().getCps().getBean(UserServiceImpl.class);
Alluser login = us.login(acc, pwd);
System.out.println(login);
// 给浏览器做个响应
resp.getWriter().write("登陆成功");
}
service层
package com.cykj.service.impl;
import com.cykj.dao.impl.UserDaoImpl;
import com.cykj.pojo.Alluser;
import com.cykj.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
@Service
public class UserServiceImpl implements UserService {
//给name赋值
@Value("zhangsan")
private String name;
//注入的是基本数据类型的属性,导入dao层
@Autowired
private UserDaoImpl userDao;
public Alluser login(String acc, String pwd) {
//调用dao层的登录
userDao.login(acc, pwd);
//简单打印一下
System.out.println("这是UserServiceImpl---" + name);
return null;
}
}
dao层
package com.cykj.dao.impl;
import com.cykj.dao.UserDao;
import com.cykj.pojo.Alluser;
import org.springframework.stereotype.Repository;
//给添加值,如果没有添加值,默认为小驼峰模式,也就是userDao名
@Repository(value = "myUserDao")
public class UserDaoImpl implements UserDao {
@Override
public Alluser login(String acc, String pwd) {
System.out.println("被调用啦!");
return null;
}
}
pojo层
package com.cykj.pojo;
import org.springframework.stereotype.Component;
@Component
public class Alluser {
private String account;
private String nickname;
public Alluser() {
}
public Alluser(String account, String nickname) {
this.account = account;
this.nickname = nickname;
}
@Override
public String toString() {
return "Alluser{" +
"account='" + account + '\'' +
", nickname='" + nickname + '\'' +
'}';
}
}
main包(测试类)
package com.cykj.main;
import com.cykj.service.impl.UserServiceImpl;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class Main {
public static void main(String[] args) {
// 加载一下IOC容器
ClassPathXmlApplicationContext cps = new ClassPathXmlApplicationContext("spring-ioc.xml");
UserServiceImpl userService = (UserServiceImpl)cps.getBean("userServiceImpl");
userService.login("1","2");
}
}
简单代码的实现就是这样子