Hello Spring(3)ConnectionUtility

ConnectionUtility.java

 1 package test;
 2 
 3 import java.sql.Connection;
 4 import java.sql.DriverManager;
 5 
 6 public class ConnectionUtility {
 7     
 8     private String userName;
 9     private String password;
10     private String dbUrl;
11     private String dbDriver;
12     
13     public Connection getConnection(){
14         
15         try {
16             Class.forName(dbDriver);
17             return DriverManager.getConnection(dbUrl, userName, password);
18         } catch (Exception e) {
19             // TODO Auto-generated catch block
20             e.printStackTrace();
21         }
22         
23         return null ;
24         
25     }
26     
27     
28     public String getUserName() {
29         return userName;
30     }
31     public void setUserName(String userName) {
32         this.userName = userName;
33     }
34     public String getPassword() {
35         return password;
36     }
37     public void setPassword(String password) {
38         this.password = password;
39     }
40     public String getDbUrl() {
41         return dbUrl;
42     }
43     public void setDbUrl(String dbUrl) {
44         this.dbUrl = dbUrl;
45     }
46     public String getDbDriver() {
47         return dbDriver;
48     }
49     public void setDbDriver(String dbDriver) {
50         this.dbDriver = dbDriver;
51     }
52     
53     
54 
55 }

 beans.xml

 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <!DOCTYPE beans PUBLIC "" "http://www.springframework.org/dtd/spring-beans.dtd" >
 3 <beans>
 4 
 5     <bean id="conn" class="test.ConnectionUtility">
 6         <property name="userName" value="root"></property>
 7         <property name="password" value="root"></property>
 8         <property name="dbUrl" value="jdbc:mysql://192.168.1.136:3306/db_epolice"></property>
 9         <property name="dbDriver" value="com.mysql.jdbc.Driver"></property>
10     </bean>
11 
12 </beans>

 

Tester.java

 1 package test;
 2 
 3 import java.sql.Connection;
 4 
 5 import org.springframework.beans.factory.BeanFactory;
 6 import org.springframework.beans.factory.xml.XmlBeanFactory;
 7 import org.springframework.core.io.ClassPathResource;
 8 import org.springframework.core.io.Resource;
 9 
10 @SuppressWarnings("deprecation")
11 public class Tester {
12     
13     public static void main( String[] args){
14         
15         Resource r = new ClassPathResource("beans.xml");
16         BeanFactory factory = new XmlBeanFactory(r);
17         
18         ConnectionUtility util = (ConnectionUtility) factory.getBean("connectionUtility");
19         Connection conn = util.getConnection();
20         
21         System.out.println( conn );
22         
23     }
24 
25 }

 

result

 

转载于:https://www.cnblogs.com/livon/archive/2013/03/26/2982714.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: 你好!以下是回答: 您好,我可以为您提供一个简单的 HelloSpring 的示例代码: 1. 首先,您需要在 pom.xml 文件中添加 Spring 的依赖: <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>5.2.6.RELEASE</version> </dependency> 2. 接着,您需要创建一个 Spring 的配置文件 applicationContext.xml,内容如下: <?xml version="1." 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"> <bean id="helloSpring" class="com.example.HelloSpring"> <property name="message" value="Hello, Spring!"/> </bean> </beans> 3. 然后,您需要创建一个 HelloSpring 类,代码如下: package com.example; public class HelloSpring { private String message; public void setMessage(String message) { this.message = message; } public void printMessage() { System.out.println(message); } } 4. 最后,您可以在主函数中调用 HelloSpring 类的 printMessage 方法,代码如下: package com.example; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; public class Main { public static void main(String[] args) { ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml"); HelloSpring helloSpring = (HelloSpring) context.getBean("helloSpring"); helloSpring.printMessage(); } } 这样,当您运行 Main 类时,就会输出 "Hello, Spring!"。希望这个示例能够帮助您入门 Spring。 ### 回答2: 编写一个HelloSpring的程序: 1. 首先,我们需要创建一个Spring项目。可以使用Spring Boot来简化项目的创建和配置。在IDE中选择创建Spring Boot项目,并选择使用Maven或者Gradle来管理项目依赖。 2. 创建一个名为HelloSpring的Java类,用于处理HTTP请求。这个类上需要加上@Controller注解,以表示它是一个控制器。 3. 在HelloSpring类中,创建一个@RequestMapping注解的方法,用于处理HTTP GET请求并返回一个简单的字符串。可以使用@ResponseBody注解,将返回的字符串直接作为Response的内容返回给客户端。 4. 在主类中添加@SpringBootApplication注解,以标识这是一个Spring Boot应用程序。在主类中,使用SpringApplication类的run()方法来启动应用程序。 5. 使用浏览器或者其他工具发送一个HTTP GET请求到http://localhost:8080/hellospring或者http://localhost:8080/,应该能够看到返回的字符串"Hello, Spring!"。 6. 可以通过修改RequestMapping的路径,或者添加参数来进行更多操作,如处理不同的HTTP请求方法、接收请求参数等。 7. 可以在配置文件中配置应用程序的端口号和其他相关配置项。在application.properties或者application.yml文件中添加配置项,如server.port = 8080。 8. 最后,可以部署应用程序到服务器上,以便可以通过互联网访问。可以通过生成WAR文件并将其部署到Tomcat等支持Java的Web容器上,或者使用云平台提供的部署服务。 通过以上步骤,就可以创建一个简单的HelloSpring应用程序。 ### 回答3: HelloSpring是一个基于Java语言的开源框架,用来简化Java应用程序开发的过程。它提供了诸多的功能和特性,可以帮助开发者更加便捷地构建可靠、高效的企业级应用程序。 使用HelloSpring框架,我们可以实现面向对象编程的概念,并通过依赖注入和控制反转的特性来降低模块之间的耦合度,提高代码的可维护性。 在编写一个HelloSpring应用程序时,我们需要通过配置文件告诉框架如何初始化和管理各个组件。首先,我们需要定义一个Java类,并使用注解来标识该类为一个可以被Spring管理的Bean。然后,我们需要在配置文件中声明这个Bean,并指定它的作用域、依赖关系和其他一些属性。 接下来,我们可以使用Spring的IoC容器来获取这个Bean,并调用它的方法或访问它的属性。通过依赖注入,我们可以将其他的Bean注入到这个Bean中,从而实现组件间的解耦。 另外,Spring还提供了AOP(面向切面编程)的功能,可以通过定义切面、切点和通知来拦截和处理应用程序的关注点。这种方式可以使得我们将一些通用的功能(如日志、事务管理等)从业务逻辑中抽离出来,提高代码的可重用性和可测试性。 总的来说,HelloSpring是一个功能丰富、易于使用的Java开发框架,可以帮助我们构建优秀的企业级应用程序。无论是小型项目还是大型系统,使用Spring都能够提供良好的开发体验和高效的开发效率。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值