spring入门程序

1.导入jar包

1.spring-core.jar这个jar 文件包含Spring 框架基本的核心工具类。Spring 其它组件要都要使用到这个包里的类,是其它组件的基本核心,当然你也可以在自己的应用系统中使用这些工具类。外部依赖Commons Logging, (Log4J)。

      2.spring-beans.jar这个jar 文件是所有应用都要用到的,它包含访问配置文件、创建和管理bean 以及进行Inversion of Control / Dependency Injection(IoC/DI)操作相关的所有类。如果应用只需基本的IoC/DI 支持,引入spring-core.jar 及spring-beans.jar 文件就可以了。外部依赖spring-core,(CGLIB)。

      3.spring-aop.jar这个jar 文件包含在应用中使用Spring 的AOP 特性时所需的类和源码级元数据支持。使用基于AOP 的Spring特性,如声明型事务管理(Declarative Transaction Management),也要在应用里包含这个jar包。外部依赖spring-core, (spring-beans,AOP Alliance, CGLIB,Commons Attributes)。

     4.spring-context.jar这个jar 文件为Spring 核心提供了大量扩展。可以找到使用Spring ApplicationContext特性时所需的全部类,JDNI 所需的全部类,instrumentation组件以及校验Validation 方面的相关类。
外部依赖spring-beans, (spring-aop)。

    5.spring-jdbc.jar这个jar 文件包含对Spring 对JDBC 数据访问进行封装的所有类。外部依赖spring-beans。

    6.spring-expression-3.2.2.jar 支持spring表达式语言。

    7.spring-tx.3.2.2.jar spring提供对事务的支持,事务的相关处理以及实现类就在这个Jar包中

    8.spring-test-3.2.2.jar spring对JUnit框架的简单封装。

    9.mysql-connector-java-5.1.28-bin.jar加载数据库驱动。

   10.commons-logging-1.2.jar日志的jar包。

2.项目结构


3.resources中applicationContent.xml的配置




        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-3.0.xsd">
       
       























      

4.test类

public class TestDemo {
public static void main(String[] args) {
//加载配置文件,配置文件路径一定要对
ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");
//从配置文件中获取jdbcTemplate对象
JdbcTemplate jdbcTemplate = (JdbcTemplate) applicationContext.getBean("jdbcTemplate");
//执行方法
jdbcTemplate.execute("create table user4 (id int primary key auto_increment,name varchar(20))");
}


@Test
public void testDemo() {
//加载配置文件,配置文件路径一定要对
ApplicationContext applicationContext = new ClassPathXmlApplicationContext(
"applicationContext.xml");
//从spring框架柱获取UserDao对象
UserDao bean = (UserDao) applicationContext.getBean("userDao");


System.out.println(bean);
}
}

5.总结:

   1.上述例子主要针对spring的简单应用,用spring框架的IOC和DI来创建对象的。

   2.对于初学者,切记不能导入所有jar包,要根据需要导入jar包,了解每个jar的作用及依赖关系。这点非常重要!

   3.配置文件的文件夹是source folder目录下,而不是folder目录。

   4.检查加载配置文件驱动时,文件路径是否正确。

   5.再次提醒检查jar包!!


你可能感兴趣的:(框架)