课堂代码提交-spring Module

This commit is contained in:
Levi 2024-11-09 13:29:44 +08:00
parent caa5f21d4c
commit efceb80e59
6 changed files with 158 additions and 0 deletions

32
spring/pom.xml Normal file
View File

@ -0,0 +1,32 @@
<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.LiLinGui.spring</groupId>
<artifactId>spring</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>jar</packaging>
<name>spring</name>
<url>http://maven.apache.org</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>6.1.12</version>
</dependency>
</dependencies>
</project>

View File

@ -0,0 +1,33 @@
package com.LiLinGui.spring;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
/**
* Hello world!
*
*/
public class App
{
public static void main( String[] args )
{
// UserDao ud = new UserDao();
// ud.findUser();
// spring创建对象在xml文件中
// 从spring容器中取出对象 -> 解耦 控制反转
ApplicationContext applicationContext = new ClassPathXmlApplicationContext("app.xml");
// UserDao userDao = (UserDao) applicationContext.getBean("UserDao");
// userDao.findUser();
// UserService userService = (UserService) applicationContext.getBean("UserService");
//// 调用者注入值
//// userService.setUserDao(userDao);
// userService.findUser();
UserDao userDao = (UserDao) applicationContext.getBean("userDao");
userDao.findUser();
UserService userService = (UserService) applicationContext.getBean("userService");
userService.findUser();
}
}

View File

@ -0,0 +1,13 @@
package com.LiLinGui.spring;
import org.springframework.stereotype.Component;
//Component Spring容器创建对象 对象名字id= userDao
//@Component = @Servise = @Controller = @Repository
@Component
//数据库操作的类习惯性命名为Dao
public class UserDao {
public void findUser(){
System.out.println("FindUser");
}
}

View File

@ -0,0 +1,25 @@
package com.LiLinGui.spring;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
@Component
public class UserService {
// Autowired Spring自动注入 + setter + getter
@Autowired
private UserDao userDao;
// public UserDao getUserDao() {
// return userDao;
// }
//
// public void setUserDao(UserDao userDao) {
// this.userDao = userDao;
// }
public void findUser(){
userDao.findUser();
}
}

View File

@ -0,0 +1,17 @@
<?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">
<!--开启注解-->
<context:component-scan base-package="com.LiLinGui.spring"/>
<!-- <bean id="UserDao" class="com.LiLinGui.spring.UserDao"></bean>-->
<!-- <bean id="UserService" class="com.LiLinGui.spring.UserService">-->
<!-- &lt;!&ndash;spring容器注入对象-->
<!-- 依赖注入从Spring容器角度来看Spring容器负责将被依赖的对象复制给调用者的成员变量相当于为调用者注入它所依赖的实例&ndash;&gt;-->
<!-- <property name="userDao" ref="UserDao"></property>-->
<!-- </bean>-->
<!-- <bean id="UserService" class="com.LiLinGui.spring.UserService" autowire="byName"></bean>-->
</beans>

View File

@ -0,0 +1,38 @@
package com.LiLinGui.spring;
import junit.framework.Test;
import junit.framework.TestCase;
import junit.framework.TestSuite;
/**
* Unit test for simple App.
*/
public class AppTest
extends TestCase
{
/**
* Create the test case
*
* @param testName name of the test case
*/
public AppTest( String testName )
{
super( testName );
}
/**
* @return the suite of tests being tested
*/
public static Test suite()
{
return new TestSuite( AppTest.class );
}
/**
* Rigourous Test :-)
*/
public void testApp()
{
assertTrue( true );
}
}