diff --git a/spring/pom.xml b/spring/pom.xml new file mode 100644 index 0000000..e7cdb3e --- /dev/null +++ b/spring/pom.xml @@ -0,0 +1,32 @@ + + 4.0.0 + + com.LiLinGui.spring + spring + 1.0-SNAPSHOT + jar + + spring + http://maven.apache.org + + + UTF-8 + + + + + junit + junit + 3.8.1 + test + + + + org.springframework + spring-context + 6.1.12 + + + + diff --git a/spring/src/main/java/com/LiLinGui/spring/App.java b/spring/src/main/java/com/LiLinGui/spring/App.java new file mode 100644 index 0000000..a0670cb --- /dev/null +++ b/spring/src/main/java/com/LiLinGui/spring/App.java @@ -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(); + } +} diff --git a/spring/src/main/java/com/LiLinGui/spring/UserDao.java b/spring/src/main/java/com/LiLinGui/spring/UserDao.java new file mode 100644 index 0000000..d82c72e --- /dev/null +++ b/spring/src/main/java/com/LiLinGui/spring/UserDao.java @@ -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"); + } +} diff --git a/spring/src/main/java/com/LiLinGui/spring/UserService.java b/spring/src/main/java/com/LiLinGui/spring/UserService.java new file mode 100644 index 0000000..d4400fe --- /dev/null +++ b/spring/src/main/java/com/LiLinGui/spring/UserService.java @@ -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(); + } + +} diff --git a/spring/src/main/resources/app.xml b/spring/src/main/resources/app.xml new file mode 100644 index 0000000..b9a2aca --- /dev/null +++ b/spring/src/main/resources/app.xml @@ -0,0 +1,17 @@ + + + + + + + + + + + + + + \ No newline at end of file diff --git a/spring/src/test/java/com/LiLinGui/spring/AppTest.java b/spring/src/test/java/com/LiLinGui/spring/AppTest.java new file mode 100644 index 0000000..80bc1a1 --- /dev/null +++ b/spring/src/test/java/com/LiLinGui/spring/AppTest.java @@ -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 ); + } +}