Spring 容器创建 依赖注入 web整合 注解使用
来源:互联网 发布:随机抽奖软件 编辑:程序博客网 时间:2024/06/05 21:54
知道为什么这样做 ,以及这样做的好处
Spring 介绍
Spring 容器就是用来创建bean对象的容器,对象从Spring容器中获得, 有两大特性 IOC AOP
1. Spring容器的创建
1.1.1 搭建开发环境jar包导入,添加约束
<!-- jdk 1.8 --> <build> <plugins> <!-- maven的编译插件,用于指定我们jdk的版本 --> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <configuration> <source>1.8</source> <target>1.8</target> <encoding>UTF-8</encoding> </configuration> </plugin> </plugins> </build> <!-- 依赖包||||||||||||||||||||||||||||| --> <dependencies> <!-- log包 4 --> <dependency> <groupId>org.bgee.log4jdbc-log4j2</groupId> <artifactId>log4jdbc-log4j2-jdbc4.1</artifactId> <version>1.16</version> </dependency> <dependency> <groupId>org.slf4j</groupId> <artifactId>slf4j-api</artifactId> <version>1.7.13</version> </dependency> <dependency> <groupId>org.slf4j</groupId> <artifactId>slf4j-log4j12</artifactId> <version>1.7.5</version> </dependency> <!-- https://mvnrepository.com/artifact/log4j/log4j --> <dependency> <groupId>log4j</groupId> <artifactId>log4j</artifactId> <version>1.2.16</version> </dependency> <!-- spring 4个包 |||||||||||||||||||||||||| --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>4.2.4.RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-core</artifactId> <version>4.2.4.RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-beans</artifactId> <version>4.2.4.RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-expression</artifactId> <version>4.2.4.RELEASE</version> </dependency>
约束添加 –
为什么这么添加不知道?? 约束的作用 意义是什么?
<?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">
1.1.2 创建容器 applicationContext.xml 容器
<bean id="personid" class="com.ruirui.dao.Person"> </bean>
1.1.3 创建对象代码
//创建spring容器 ApplicationContext applicationContext =new ClassPathXmlApplicationContext("beans.xml"); //从容器中获取对象 Person bean = (Person) applicationContext.getBean("personid"); System.out.println(bean.toString());
1.2.1 创建Spring对象的三种方式
第一种
第二种 静态工厂获取对象
第三种 实例工厂获取对象
2. 依赖注入
2.1 get set方法注入
<bean id="personid" class="com.ruirui.dao.Person" init-method="init" destroy-method="destory"> <property name="name" value="刘广睿"></property> <property name="sex" value="男"></property> <property name="hairColor" value="黑色"></property> <property name="age" value="27"></property> </bean>
2.2 构造方法 引用类型注入
<bean id="superperson" class="com.ruirui.dao.SuperPerson"> <property name="name" value="路费"></property> <property name="address" value="东海"></property> </bean> <bean id="personid_two" class="com.ruirui.dao.Person" > <constructor-arg name="superPerson" ref="superperson"></constructor-arg> <constructor-arg name="name" value="冬雪"></constructor-arg> </bean>
2.3 set list map properies 注入
<property name="list" > <list> <value>1</value> <value>2</value> <value>"你好北京"</value> </list> </property> <property name="set"> <set> <value>周杰伦</value> <value>陈奕迅</value> </set> </property> <property name="map"> <map> <entry key="tom" value="tom"></entry> <entry key="mary" value="mary"></entry> <entry key="rose" value="mary"></entry> </map> </property> <property name="properties"> <props> <prop key="1">1</prop> <prop key="2">1</prop> <prop key="3">1</prop> </props> </property>
2.4 p标签 同c标签使用
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p" xmlns:c="http://www.springframework.org/schema/c" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"> <bean id="person_p" class="com.ruirui.dao.Person" p:name="索隆" p:age="29"></bean> <bean id ="superPersonID" class="com.ruirui.dao.SuperPerson" ></bean> <bean id="person_p_P" class="com.ruirui.dao.Person" c:name="索隆" c:superPerson="#{superPersonID}"></bean>
3. Web整个
3.1 添加两个jar包
<!-- spring 跟 web 整个 需要导入两个依赖包 spring-web servlet包 --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-web</artifactId> <version>4.2.4.RELEASE</version> </dependency> <!-- 导入servlet的包 --> <dependency> <groupId>javax.servlet</groupId> <artifactId>javax.servlet-api</artifactId> <version>3.1.0</version> <scope>provided</scope> </dependency>
3.2 配置监听器
<!-- web-spring 自带的监听器配置 --> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> <context-param> <param-name>contextConfigLocation</param-name> <!-- 这个有个路径问题 等会部署的时候去tomcat看看 --> <param-value>classpath:spring_web_bean.xml</param-value> </context-param>
3.3 从request预中 获取spring容器中所有数据
* Servlet implementation class MyServlet */public class MyServlet extends HttpServlet { protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // TODO Auto-generated method stub response.getWriter().append("Served at: ").append(request.getContextPath()); WebApplicationContext attribute = (WebApplicationContext) request.getServletContext().getAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE); SuperPerson bean = (SuperPerson) attribute.getBean("person_p"); System.out.println(bean.toString()); }
4. 注解的使用
4.1 导入注解包
<!-- https://mvnrepository.com/artifact/org.springframework/spring-aop --><dependency> <groupId>org.springframework</groupId> <artifactId>spring-aop</artifactId> <version>4.2.4.RELEASE</version></dependency>
在bean.xml中需要添加约束,并且配置需要扫描的包,告诉spring容器开启注解模式
<!--刚才这里缺一个约束 --> http://www.springframework.org/schema/context<context:annotation-config /> <context:component-scan base-package="com.ruirui.dao"></context:component-scan>
常用的注解
第一个注解:@component :该类可以被注解创建实例第二个注解:@Value 给基本数据类型赋值第三个注解: @Autowired 用于注入对象属性@Resource 给引用数据类型定义ID@Controller:用于web层@Service :用于业务逻辑层@Reposiroty :用于Dao层@Autowired+@Qualifier === @Resource
Spring配置文件
<!-- 代表采用注解方式创建实例 --> <context:annotation-config /> <!-- 扫描这个包下面的所有注解 --> <context:component-scan base-package="com.ruirui.dao"></context:component-scan>
注解的使用
Person类
@Component(value="person") //person 代表的是对象的IDpublic class Person { @Value(value="100") //value 代表给成员变量赋值 private Integer age; private String hairColor; private String sex ; @Resource(name="superson") private SuperPerson superPerson; // Person内部的引用数据类型定义id superson
@Component(value="superson")public class SuperPerson { public void fly() { System.out.println("会飞"); }}
阅读全文
0 0
- Spring 容器创建 依赖注入 web整合 注解使用
- Spring使用注解依赖注入
- Spring:IOC释义(Bean容器、注解、依赖注入)
- Spring依赖注入:注解注入
- Spring Ioc容器依赖注入
- spring annotation注解 依赖注入
- spring依赖注入(2)-注解
- spring依赖注入的注解
- ASP.NET Web Application 中使用 Unity 依赖注入容器
- spring使用注解进行DI(依赖)注入
- 有关spring的依赖注入和@resource注解的使用
- Spring依赖注入:注解注入总结
- Spring依赖注入:注解注入总结
- Spring依赖注入:注解注入总结
- Spring依赖注入:注解注入总结
- Spring依赖注入:注解注入总结
- Spring依赖注入:注解注入总结
- Spring依赖注入:注解注入总结
- 排序算法之插入排序(Java)
- 如何自定义注解实现简单的权限控制
- 小白算法练习 简单背包问题专题004 多重背包 二进制化 POJ dp
- P1433 吃奶酪
- 热传播测地线距离的计算
- Spring 容器创建 依赖注入 web整合 注解使用
- C++中的类内存布局②---补充(12)《Effective C++》
- php 实现验证码
- spfa 的优化
- HDU 1394 Minimum Inversion Number(线段树,单节点修改,区间求和)
- Java经典编程—古典兔子
- 5分钟了解swagger
- 【C++】【学习笔记】/*函数声明的四种方式*/
- maven学习