Spring概念

1spring是开源的轻量级框架

2spring核心主要两部分

1)Aop:面向切面编程,扩展功能不是修改源代码实现

2)Ioc:控制反转,

    -比如有个类,在类里面有方法(不是静态的方法),调用类里面的方法,创建类的对象,使用对象调用方法,创建类对象的过程,需要出来对象

    -把对象的创建不是通过方式实现,而是交给配置创建类对象

3spring是一站式框架

1)spring在javaee三层结构中,每一层都提供不同的解决技术

-web层 springMVC

-service层 spring的ioc

-dao层 spring的jdbcTemplate

4 spring版本

(1)hibernate5.x

(2)Spring4.x

Spring的IOC操作

1 把对象的创建交给spring进行管理

2 ioc操作两部分

1)ioc的配置文件方式

2)Ioc懂得注解方式

Ioc底层原理

1 ioc底层原理 使用技术

1)xml配置文件

2)Dom4j解决xml

3)工厂设计模式

4)反射

入门案列

第一步导入jar包

包文件.png

   第二步 创建类和类中的方法

package com.netxintai.domain;

public class User {
		private String username;
		private String password;
		
		public void add(){
			
			System.out.println("添加用户操作");
		}
}

   第三步引入约束文件

<?xml version="1.0" 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="user" class="com.netxintai.domain.User"></bean>
 </beans>

第四步创建测试方法和获取对象

package com.netxintai.action;

import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.netxintai.domain.User;

public class TestApplicaion {
	
	
	@Test
	public void test(){
		
		ApplicationContext context=new ClassPathXmlApplicationContext("applicationContext.xml");
		
		User user=(User) context.getBean("user");
		user.add();
	}

}

控制台输出测试结果

image.png

发表回复

您的电子邮箱地址不会被公开。 必填项已用*标注

Post Navigation