This commit is contained in:
commit
4b85734784
|
@ -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.hnucm.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>
|
|
@ -0,0 +1,33 @@
|
|||
package com.hnucm.spring;
|
||||
|
||||
import org.springframework.context.ApplicationContext;
|
||||
import org.springframework.context.support.ClassPathXmlApplicationContext;
|
||||
|
||||
/**
|
||||
* Hello world!
|
||||
*
|
||||
*/
|
||||
public class App
|
||||
{
|
||||
public static void main( String[] args ) {
|
||||
// UserDao userDao2 = new UserDao();
|
||||
// userDao.findUser();
|
||||
// Spring容器创建对象xml文件中
|
||||
|
||||
// Spring容器中取出对象 -》 解耦 控制反转
|
||||
// 在使用Spring框架之后,对象的实例不再由调用者来创建,而是由Spring容器来创建
|
||||
|
||||
ApplicationContext applicationContext =
|
||||
new ClassPathXmlApplicationContext("app.xml");
|
||||
|
||||
UserDao userDao = (UserDao) applicationContext.getBean("userDao");
|
||||
userDao.findUser();
|
||||
|
||||
UserService userService = (UserService) applicationContext.getBean("userService");
|
||||
// // 调用注入值 -》 Spring 注入
|
||||
// // userService.setUserDao(userDao);
|
||||
userService.findUser();
|
||||
}
|
||||
|
||||
|
||||
}
|
|
@ -0,0 +1,15 @@
|
|||
package com.hnucm.spring;
|
||||
|
||||
|
||||
import org.springframework.stereotype.Component;
|
||||
import org.springframework.stereotype.Repository;
|
||||
|
||||
// Component Spring容器创建UserDao对象 对象的名字id = userDao
|
||||
@Repository
|
||||
public class UserDao {
|
||||
|
||||
// 调用findUser()
|
||||
public void findUser(){
|
||||
System.out.println("查询用户");
|
||||
}
|
||||
}
|
|
@ -0,0 +1,23 @@
|
|||
package com.hnucm.spring;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Component;
|
||||
import org.springframework.stereotype.Controller;
|
||||
import org.springframework.stereotype.Repository;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
// id = userService
|
||||
// @Component =Service =Controller =Repository
|
||||
@Service
|
||||
public class UserService {
|
||||
|
||||
// Autowired Spring自动注入 + setter+getter
|
||||
@Autowired
|
||||
private UserDao userDao;
|
||||
|
||||
|
||||
// 如何调用Spring容器 finduser
|
||||
public void findUser(){
|
||||
userDao.findUser();
|
||||
}
|
||||
}
|
|
@ -0,0 +1,22 @@
|
|||
<?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.hnucm.spring"/>
|
||||
|
||||
<!-- Spring容器会自动创建对象 UserDao userDao = new UserDao(); -->
|
||||
<!--<bean id="userDao" class="com.hnucm.spring.UserDao">-->
|
||||
|
||||
<!--</bean>-->
|
||||
<!--<!–userDao–>-->
|
||||
<!--<bean id="userService" class="com.hnucm.spring.UserService"-->
|
||||
<!-- autowire="byName">-->
|
||||
<!-- <!– Spring容器注入对象 –>-->
|
||||
<!-- <!–从Spring容器角度来看,Spring容器负责将被依赖的对-->
|
||||
<!-- 象复制给调用者的成员变量,相当于为调用者注入它所依赖的实例,这就是Spring的依赖注入。–>-->
|
||||
<!-- <!–<property name="userDao" ref="userDao"></property>–>-->
|
||||
<!--</bean>-->
|
||||
|
||||
</beans>
|
|
@ -0,0 +1,38 @@
|
|||
package com.hnucm.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 );
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue