DI 依赖注入=》构造方式的形式
构造方法的注入,使用实体类对象进行注入 Student类
集合的注入(数组、List、Set、Map)
<bean id ="student" class ="com.neuedu.pojo.Student" >
<constructor-arg index ="0" type ="java.lang.String" value ="Tom" />
<constructor-arg index ="1" type ="java.lang.String" value ="男" />
<constructor-arg index ="2" type ="int" value ="20" />
<property name ="arrData" >
<array >
<value > 100</value >
<value > 200</value >
<value > 300</value >
</array >
</property >
<property name ="listData" >
<list >
<value > list002</value >
<value > list003</value >
<value > list001</value >
</list >
</property >
<property name ="setData" >
<set >
<value > set001</value >
<value > set001</value >
</set >
</property >
<property name ="mapData" >
<map >
<entry key ="k01" value ="v01" />
<entry key ="k02" value ="v02" />
</map >
</property >
</bean >
注册Bean对象基于注解形式
a) 注解:就是一个类,使用的格式:@注解名称
b) @Component 取代 xml文件
c) @Component(“id内容”) 取代
d) Web开发中,通过@Component注解衍生出来的注解
i. @Repository: 主要标注的是dao层
ii. @Service: 主要标注的是service层
iii. @Controller 主要标注的是web层(servlet)
e) 依赖的注入=》注解
i. 普通值:@Value
ii. 引用值 (ref)
按类型进行注入
a) @Autowired
f) 使用注解时,需要在spring核心的配置文件中,配置注解类所在的包
注解案例:
a) 依赖的引入、核心配置文件的创建
b) 创建controller/service/dao类并进行注解的使用
controller ---@Controller
package com.bboy .controller ;
import com.bboy .service .StudentService ;
import com.bboy .service .impl .StudentServiceImpl ;
import org.springframework .beans .factory .annotation .Autowired ;
import org.springframework .stereotype .Component ;
@Component ("studentController" )
public class StudentController {
@Autowired
private StudentService studentService;
public void listStudents ( ){
studentService.listStudents ();
}
public StudentService getStudentService ( ) {
return studentService;
}
public void setStudentService (StudentService studentService ) {
this .studentService = studentService;
}
}
dao --- @Repository("studentDao")
package com.bboy.dao.impl;
import com.bboy.dao.StudentDao;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Repository;
@Repository("studentDao")
public class StudentDaoImpl implements StudentDao {
@Override
public void listStudents () {
System.out.println("StudentDaoImpl===============>>listStudents" );
}
}
servicec---@Service("studentService")
package com.bboy.service.impl;
import com.bboy.dao.StudentDao;
import com.bboy.service.StudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
@Service("studentService")
public class StudentServiceImpl implements StudentService {
@Autowired
private StudentDao studentDao;
@Override
public void listStudents () {
studentDao.listStudents();
}
}
在核心的配置文件中加载标有注解的注解类
i. 开启context命名空间 ii. 进行注解类所在包的扫描
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns ="http://www.springframework.org/schema/beans"
xmlns:context ="http://www.springframework.org/schema/context"
xmlns:xsi ="http://www.w3.org/2001/XMLSchema-instance"
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" >
<context:component-scan base-package ="com.bboy.controller" />
<context:component-scan base-package ="com.bboy.service.impl" />
<context:component-scan base-package ="com.bboy.dao.impl" />
</beans >
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 【设计模式】告别冗长if-else语句:使用策略模式优化代码结构
· 字符编码:从基础到乱码解决
· 提示词工程——AI应用必不可少的技术