深圳罗湖做网站公司哪家好,中国电信网站备案管理系统,网络做网站,描写做网站专业的句子目录
首页#xff1a;
1.Spring
1.1 简介
1.2 优点
2.IOC理论推导
3.IOC本质
4.HelloSpring
ERROR
5.IOC创建对象方式
5.1、无参构造 这个是默认的
5.2、有参构造
6.Spring配置说明
6.1、别名
6.2、Bean的配置
6.3、import
7.DL依赖注入环境
7.1 构造器注入 …目录
首页
1.Spring
1.1 简介
1.2 优点
2.IOC理论推导
3.IOC本质
4.HelloSpring
ERROR
5.IOC创建对象方式
5.1、无参构造 这个是默认的
5.2、有参构造
6.Spring配置说明
6.1、别名
6.2、Bean的配置
6.3、import
7.DL依赖注入环境
7.1 构造器注入
7.2 Set方式注入
7.3 案例代码
7.3.1.Student类 7.3.2 Address类 7.3.3 beans.xml 7.3.4 Mytest4类 首页 我是跟着狂神老师的视频内容来整理的笔记不得不说真的收获颇丰希望这篇笔记能够帮到你。
..... (¯v´¯)♥
.......•.¸.•´
....¸.•´
... ( ☻/
/▌♥♥
/ \ ♥♥
1.Spring
1.1 简介
由Rod Johnson创建雏形是interface21框架。理念是使现有的技术更加容易使用本身是一个大杂烩整合了现有的技术框架
SSH: Struct2SppringHibernateSSM:SpringMVCSpringMybatis !-- https://mvnrepository.com/artifact/org.springframework/spring-webmvc --
dependency
groupIdorg.springframework/groupId
artifactIdspring-webmvc/artifactId
version6.0.11/version
/dependency
!-- https://mvnrepository.com/artifact/org.springframework/spring-webmvc --
dependency
groupIdorg.springframework/groupId
artifactIdspring-jdbc/artifactId
version6.0.11/version
/dependency
1.2 优点 2.IOC理论推导
1.UserDao接口 2.UserDaolmpl实现类 3.UserService业务接口 4.UserServicelmpl业务实现类 上面的四个类是我们写项目时的传统的写法。主要就是在实现类中实现功能最后在业务实现类中最终实现。 通过在Servicelmpl中创建一个新的的UserDao对象是可以实现方法的调用的但是当后面所调用的类变得越来越多以后这种方法就不太适合了。比如说多了很多类似于UserDaolmpl的实现类但是想要调用他们的话就必须在其对应的Service中进行更改太过于麻烦耦合性太强。 解决方法
public class UserServicelmpl implements UserService{private UserDao userDao;//利用set进行动态实现值的注入public void setUserDao(UserDao userDao){this.userDaouserDao;}public void getUser() {userDao.getUser();}
}实现类 3.IOC本质 简言之就是把控制权交给了用户而不是程序员我们可以通过所选择的来呈现不同的页面或者说是表现方式。用户的选择变多了。
4.HelloSpring
这是一个视频里的小案例旨在加深对bean的理解。beans.xml的正规名叫做applicationContext.xml,到后面可以用Import进行导入。
代码
//1.Hello
package org.example;
public class Hello {private String str;public String getStr() {return str;}public void setStr(String str) {this.str str;}Overridepublic String toString() {return Hello{strstr\};}
}
//2.beans.xml
?xml version1.0 encodingUTF-8?
beans xmlnshttp://www.springframework.org/schema/beansxmlns:xsihttp://www.w3.org/2001/XMLSchema-instancexsi:schemaLocationhttp://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans.xsd!-- 这里的name的值就是类中变量名 --bean idhello classorg.example.Helloproperty namestr valuespring/property/bean
/beans//3.实现测试类MyTest
import org.example.Hello;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;public class MyTest {public static void main(String[] args) {//获取Spring的上下文对象ApplicationContext context new ClassPathXmlApplicationContext(beans.xml);Hello hello (Hello) context.getBean(hello); //这里的hello就是创建对象的变量名System.out.println(hello.toString());}
} idea中自动生成返回对象的快捷键 ctraltv ERROR
1. 原因JDK版本过低造成要大于1.8我用的2.0
5.IOC创建对象方式
5.1、无参构造 这个是默认的
bean iduser classorg.example.pojo.User property namename value张总/property /bean
5.2、有参构造
通过下标获得
bean iduser classorg.example.pojo.User constructor-arg index0 value王总/ /bean
通过变量的类型获得但不建议用因为当变量名有很多时便不适用了
bean iduser classorg.example.pojo.User constructor-arg typejava.lang.String value赵总/ /bean
通过变量名来获得
bean iduser classorg.example.pojo.User constructor-arg namename value李总/ /bean
6.Spring配置说明
6.1、别名
起别名并不是覆盖原有的变量名 6.2、Bean的配置 6.3、import 7.DL依赖注入环境
7.1 构造器注入
前面已经说过了。
7.2 Set方式注入
依赖注入Set注入 1.依赖bean对象的创建依赖于容器spring 2.注入bean对象中的所有属性由容器来注入
7.3 案例代码
一个比较全的案例包括了String,类数组list集合MapSet,Null,Properties。
代码如下
7.3.1.Student类
//1.Student
package org.example;import java.util.*;public class Student {private String name;private Address address;private String[] books;private ListString hobbys;private MapString,String card;private SetString games;private String wife; //空指针private Properties info; //不是很理解这个的意思public String getName() {return name;}public void setName(String name) {this.name name;}public Address getAddress() {return address;}public void setAddress(Address address) {this.address address;}public String[] getBooks() {return books;}public void setBooks(String[] books) {this.books books;}public ListString getHobbys() {return hobbys;}public void setHobbys(ListString hobbys) {this.hobbys hobbys;}public MapString, String getCard() {return card;}public void setCard(MapString, String card) {this.card card;}public SetString getGames() {return games;}public void setGames(SetString games) {this.games games;}public String getWife() {return wife;}public void setWife(String wife) {this.wife wife;}public Properties getInfo() {return info;}public void setInfo(Properties info) {this.info info;}Overridepublic String toString() {return Student{namename\,addressaddress.toString(),books Arrays.toString(books),hobbyshobbys,cardcard,gamesgames,wifewife\,infoinfo};}
}7.3.2 Address类
//2.Address类
package org.example;public class Address {private String address;public String getAddress() {return address;}public void setAddress(String address) {this.address address;}Overridepublic String toString() {return address;}
}7.3.3 beans.xml
//3.beans.xml
?xml version1.0 encodingUTF-8?
beans xmlnshttp://www.springframework.org/schema/beansxmlns:xsihttp://www.w3.org/2001/XMLSchema-instance xmlns:utilhttp://www.springframework.org/schema/utilxsi:schemaLocationhttp://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/util https://www.springframework.org/schema/util/spring-util.xsdbean idaddress classorg.example.Addressproperty nameaddressvalue西安/value/property/beanbean idstudent classorg.example.Studentproperty namename value秦三/property nameaddress refaddress/property namebooksarrayvalue语文/valuevalue数学/valuevalue英语/valuevalue化学/value/array/propertyproperty namehobbyslistvalue篮球/valuevalue足球/valuevalue台球/value/list/propertyproperty namecardmapentry key身份证 value1111111111111/entry key银行卡 value2222222222222//map/propertyproperty namegamessetvalueLOL/valuevalueCOC/value/set/propertyproperty namewifenull//propertyproperty nameinfopropsprop key学号12345/propprop key性别男/propprop key姓名张三/prop/props/property/bean!-- more bean definitions go here --/beans 7.3.4 Mytest4类
//4.MyTest测试类
import org.example.Student;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;public class MyTest4 {public static void main(String[] args) {ApplicationContext context new ClassPathXmlApplicationContext(beans.xml);Student student (Student) context.getBean(student);System.out.println(student.toString());}
}最后祝大家身体健康学习快乐天天向上