Spring入门_01
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | < bean id="userAction" class="com.umgsai.spring.UserAction"> < property name="userManager" ref="manager"></ property > <!-- 依赖注入 --> </ bean > < bean id="manager" class="com.umgsai.spring.UserManager"> < property name="driverClass"> < value >com.microsoft.sqlserver.jdbc.SQLServerDriver</ value > </ property > < property name="username"> < value >sa</ value > </ property > < property name="password"> < value >123456</ value > </ property > < property name="url"> < value >jdbc:sqlserver://localhost:1433;database=surveillance</ value > </ property > </ bean > |
UserManager.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 | package com.umgsai.spring; public class UserManager { private String username; private String password; private String url; private String driverClass; public void setUsername(String username) { this .username = username; } public void setPassword(String password) { this .password = password; } public void setUrl(String url) { this .url = url; } public void setDriverClass(String driverClass) { this .driverClass = driverClass; } public void add(User user) { System.out.println(username); System.out.println(password); System.out.println(driverClass); System.out.println(url); System.out.println( "执行添加User操作" ); } } |
UserAction.java
1 2 3 4 5 6 7 8 9 10 11 12 | package com.umgsai.spring; public class UserAction { private UserManager userManager; public void setUserManager(UserManager userManager) { this .userManager = userManager; } public String execute() { User user = new User(); userManager.add(user); return "success" ; } } |
1 2 3 4 5 6 7 8 | public static void main(String[] args) { //IMessage message = MessageFactory.create("1"); //创建spring Bean工厂 BeanFactory factory = new ClassPathXmlApplicationContext( "applicationContext.xml" ); //BeanFactory factory = new ClassPathXmlApplicationContext("app*.xml");通配符 UserAction userAction = (UserAction)factory.getBean( "userAction" ); System.out.println(userAction.execute()); } |
控制台输出
sa
123456
com.microsoft.sqlserver.jdbc.SQLServerDriver
jdbc:sqlserver://localhost:1433;database=surveillance
执行添加User操作
success
本文出自 “阿凡达” 博客,请务必保留此出处http://shamrock.blog.51cto.com/2079212/1254159
积跬步以致千里,积小流以成江海。
2016年5月之前的博文发布于51cto,链接地址:shamrock.blog.51cto.com
2016年5月之后博文发布与cnblogs上。
Github地址 https://github.com/umgsai
Keep moving~!!!
2016年5月之前的博文发布于51cto,链接地址:shamrock.blog.51cto.com
2016年5月之后博文发布与cnblogs上。
Github地址 https://github.com/umgsai
Keep moving~!!!
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步