UserController.java
package com.aistart.controller;
import com.aistart.pojo.Student;
import com.aistart.service.UserService;
import com.aistart.service.impl.UserServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
@Controller
public class UserController {
/*依赖抽象*/
@Autowired
/*它的逻辑并不是根据名字去找,而是根据同源累类型去找
*
* 如果找到了两个一摸一样的同源类型???--->找名字
* */
@Qualifier("userServiceImpl")//功能是,让自动装配的寻找逻辑改为根据名字去查找
//别忘了,要与Autowired连用
UserService userService;
public void setUserService(UserService userService) {
this.userService = userService;
}
public String Login(Student student){
boolean login = userService.login(student);
if (login)return "yes";
else return "no";
}
}
UserMapperImpl.java
package com.aistart.mapper.impl;
import com.aistart.mapper.UserMapper;
import com.aistart.pojo.Student;
import org.springframework.stereotype.Repository;
@Repository
public class UserMapperImpl implements UserMapper {
@Override
public int selectUser(Student student) {
System.out.println("这里是查找是否有用户的mapper");
return 0;
}
}
School.java
package com.aistart.pojo;
public class School {
private String schoolName;
public School(String schoolName) {
this.schoolName = schoolName;
}
public School() {
System.out.println("school的无参被调用了");
}
public String getSchoolName() {
return schoolName;
}
public void setSchoolName(String schoolName) {
this.schoolName = schoolName;
}
@Override
public String toString() {
final StringBuilder sb = new StringBuilder("School{");
sb.append("schoolName='").append(schoolName).append('\'');
sb.append('}');
return sb.toString();
}
}
Student.java
package com.aistart.pojo;
import org.springframework.stereotype.Component;
/*如何将一个类注册到容器*/
/*
* 有哪些注解
*
*
* */
@Component
public class Student {
private String stuName;
private Integer age;
private School school;
public School getSchool() {
return school;
}
public void setSchool(School school) {
this.school = school;
}
public Student() {
System.out.println("student无参");
}
public Student(String stuName, Integer age) {
System.out.println("全参数构造");
this.stuName = stuName;
this.age = age;
}
public String getStuName() {
return stuName;
}
public void setStuName(String stuName) {
this.stuName = stuName;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
@Override
public String toString() {
final StringBuilder sb = new StringBuilder("Student{");
sb.append("stuName='").append(stuName).append('\'');
sb.append(", age=").append(age);
sb.append(", school=").append(school);
sb.append('}');
return sb.toString();
}
}
User1ServiceImpl.java
package com.aistart.service.impl;
import com.aistart.mapper.UserMapper;
import com.aistart.pojo.Student;
import com.aistart.service.UserService;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
@Service
public class User1ServiceImpl implements UserService {
@Resource
UserMapper userMapper;
public void setUserMapper(UserMapper userMapper) {
this.userMapper = userMapper;
}
@Override
public boolean login(Student student) {
// 具体事先
System.out.println("这是login的service业务");
/*判断各种*/
int i = userMapper.selectUser(student);
if (i==1){
return true;
}
return false;
}
}
UserServiceImpl.java
package com.aistart.service.impl;
import com.aistart.mapper.UserMapper;
import com.aistart.mapper.impl.UserMapperImpl;
import com.aistart.pojo.Student;
import com.aistart.service.UserService;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
@Service
/*起别名*/
public class UserServiceImpl implements UserService {
@Resource
UserMapper userMapper;
public void setUserMapper(UserMapper userMapper) {
this.userMapper = userMapper;
}
@Override
public boolean login(Student student) {
// 具体事先
System.out.println("这是login的service业务");
/*判断各种*/
int i = userMapper.selectUser(student);
if (i==1){
return true;
}
return false;
}
}
AnnotationContext.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
https://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
https://www.springframework.org/schema/context/spring-context.xsd">
<!--只有被注册的包下面的注解才能生效-->
<context:component-scan base-package="com.aistart"></context:component-scan>
<context:annotation-config/>
</beans>
ApplicationContext.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"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
<!--第二步,创建元数据-->
<!--id就相当于这个对象的唯一标识,理解为这对象在容器中的"变量名"-->
<!--bean中,调用默认的构造方法,去创建-->
<bean id="alice" class="com.aistart.pojo.Student">
<!--默认调用参数位置去进行赋值-->
<constructor-arg index="0" value="Candy"/>
<constructor-arg index="1" value="19"/>
<property name="stuName" value="Alice"/>
<!--标签中,相当于键值对,name:value-->
<!--name等同于属性名-->
<!--value等同于注入的值
注意,不管加入什么类型都是""引起来的-->
<property name="age" value="19"/>
<!--这个标签,其实是执行了set方法去注入的-->
<!--一定要有set方法-->
<!--name 搭配value和ref
value是简单类型,字面量能够赋值的
ref是引入类型,是外部获取得另一个对象
-->
<property name="school" ref="ksdx"/>
<!-- 这个bean的合作者和配置在这里 -->
</bean>
<bean id="sqxy" class="com.aistart.pojo.School">
<!-- 这个bean的合作者和配置在这里 -->
<property name="schoolName" value="小清华"></property>
</bean>
<bean id="ksdx" class="com.aistart.pojo.School">
<!-- 这个bean的合作者和配置在这里 -->
<property name="schoolName" value="呼呼睡觉"></property>
</bean>
<!-- 更多bean 定义在这里 -->
<bean id="userController" class="com.aistart.controller.UserController">
<property name="userService" ref="userService"></property>
<!--对应的注解就是@AutoWrite(用它多) @Resouce()-->
</bean>
<bean id="userService" class="com.aistart.service.impl.UserServiceImpl">
<property name="userMapper" ref="userMapper"></property>
</bean>
<bean id="userMapper" class="com.aistart.mapper.impl.UserMapperImpl"/>
<bean id="newOne" class="com.aistart.pojo.Student" scope="prototype">
<!-- <constructor-arg index="0" value="下名利"/>-->
<!-- <constructor-arg index="1" value="10"/>-->
<!--
调用有参构造方法
Student(string stuName,int age){
...
}
-->
<!--这里面的name值,其实是传参的参数名-->
<constructor-arg name="age" value="19"></constructor-arg>
<constructor-arg name="stuName" value="candy"></constructor-arg>
<!-- <constructor-arg name="school" ref=""-->
</bean>
</beans>
UserControllerTest.java
package com.aistart.controller;
import com.aistart.pojo.Student;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import static org.junit.Assert.*;
public class UserControllerTest {
@Test
public void login() {
/*利用spring容器获取controller对象*/
ApplicationContext context = new ClassPathXmlApplicationContext("AnnotationContext.xml");
UserController userController = (UserController) context.getBean("userController");
String login = userController.Login(new Student("小米",1));
System.out.println(login);
}
}
StudentTest.java
package com.aistart.pojo;
import org.junit.Test;
import org.springframework.beans.BeanUtils;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import static org.junit.Assert.*;
public class StudentTest {
@Test
public void test(){
/*传统的注入方式*/
/*1.构造器注入*/
Student alice = new Student("Alice", 18);
/*2.用set方法注入*/
Student student = new Student("小谬",1);
student.setStuName("Bob");
student.setAge(19);
}
/*使用spring来创建对象*/
@Test
public void beanTest(){
/*第三步,实例化一个容器*/
/*无参被调用说明:容器不单单是加载xml,并且根据xml去创建对象,全都创建了*/
/*创建的同时应该将值注入其中*/
ApplicationContext context = new ClassPathXmlApplicationContext("ApplicationContext.xml");
/*第四步,获取实例对象
*
* 参数:是xml中对象实例的id
* 返回:类型都是Object
* */
Student new1 =(Student) context.getBean("newOne");
Student new2 =(Student) context.getBean("newOne");
Student alice1 =(Student) context.getBean("alice");
/*在spring容器中,对象实例默认都是单例的*/
System.out.println(new1==new2);
}
@Test
public void annoTest(){
ApplicationContext context = new ClassPathXmlApplicationContext("AnnotationContext.xml");
// Object user = context.getBean("user");
}
}
pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.aistart</groupId>
<artifactId>Spring_demo</artifactId>
<version>1.0-SNAPSHOT</version>
<properties>
<maven.compiler.source>8</maven.compiler.source>
<maven.compiler.target>8</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<!--第一步,导入环境-->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.13.2</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>5.3.30</version>
</dependency>
</dependencies>
</project>