spring笔记二
DI—Dependency Injection,即“依赖注入”:组件之间依赖关系由容器在运行期决定,形象的说,即由容器动态的将某个依赖关系注入到组件之中。依赖注入的目的并非为软件系统带来更多功能,而是为了提升组件重用的频率,并为系统搭建一个灵活、可扩展的平台。
手动注入的setter注入
根据属性的不同类型,可以分为三种注入方式:
1)基本类型注入
2)Spring组件类型注入
3)集合类型注入
代码演示:
<bean id="boy" class="model.Boy">
<property name="age" value="18"></property> <!-- spring基本数据类型的注入 -->
<property name="name" value="张三"></property> <property name="dog" ref="dog"></property> <!-- spring bean组件的注入 --> <property name="likes" > <!-- spring 集合类型List的注入 注意有先后顺序 -->
<list>
<value type="java.lang.String">打篮球 </value>
<value type="java.lang.String">踢足球 </value>
<!--ref bean="cat"/-->
</list>
</property> <property name="likesset" > <!-- spring 集合类型Set的注入 注意没有先后顺序 -->
<set>
<value type="java.lang.String">打游戏 </value>
<value type="java.lang.String">聊天</value>
<!--ref bean="cat"/-->
</set>
</property> <property name="scoremap" > <!-- spring 集合类型Map的注入 注意没有先后顺序 -->
<map>
<entry key="语文" value="100"> </entry>
<entry key="数学" value="90"> </entry>
<!--entry key-ref="bean" value-ref=""></entry-->
</map>
</property>
</bean>
使用注解配置文件
要把spring-aop-4.1.0.RELEASE包导入项目中,这个包提供对注解的支持,如果不用注解,那就不用这个包。
xml:
<?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:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd"> <context:component-scan base-package="com.chinasofti"></context:component-scan> </beans>
@Service:用于标注业务层组件、
@Controller:用于标注控制层组件(如struts中的action)、
@Repository:用于标注数据访问组件,即DAO组件。
@Component:泛指组件,当组件不好归类的时候,我们可以使用这个注解进行标注。
@Component(value="boy")
public class Boy { @Value(value="张三")
private String name;
@Value(value="10")
private int age;
@Resource(name="dog")
private Dog dog;
Set get 方法 }
1)@Component(value="boy") 相当于把Boy加入Spring容器中,并起名叫boy
2)@Value(value="张三") 相当于setter方式,给成员变量赋值为张三
3)@Resource(name="dog")相当于
<bean id = "boy" class="com.chinasofti.Boy">
<property name="dog" ref="dog"></property>
</bean>
总结:注解使用可以代替配置文件,不过,这种注解的方式不适合应用在大项目中,因为@Component(value="boy")boy有可能会相同。
Log4J用法:
1)导入log4j-1.2.8.jar包
放在WebRoot->WEB-INF->lib中,切记,要把它加载到项目中(Build path)
2)在src目录下新建一个log4j.properties文件,此文件的功能是,设置出错信息录入方式
log4j.rootLogger=INFO,BB log4j.appender.BB=org.apache.log4j.FileAppender
log4j.appender.BB.File=D:\\test.log
log4j.appender.BB.layout=org.apache.log4j.PatternLayout
log4j.appender.BB.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} %l %F %p %m%n
3)创建一个类叫Log.java的类,写入public static Logger logger = Logger.getLogger(Log.class.getName()); 得到log4j的对象
import org.apache.log4j.Logger; public class Log {
public static Logger logger = Logger.getLogger(Log.class.getName());
}
4)当程序出异常时,就可以在catch中使用
public class Login4jServlet extends HttpServlet { public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doPost(request,response);
} public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException { Log.logger.error("进入Login4jServlet模块");
try{
String aa = request.getParameter("aa");
int count = 100/0;
int intaa = Integer.parseInt(aa);
}catch(Exception e){
Log.logger.error(e.getMessage());
request.setAttribute("errMsg","系统繁忙,请稍后连接");
request.getRequestDispatcher("error.jsp").forward(request, response);
}
}
}
运行后会在指定位置输出log文件,可以协助调试程序错误。
spring笔记二的更多相关文章
- Spring笔记(二)Core层
Spring用一种非入侵的方式来管理程序,模块结构图如下: .Core层 IOC(控制反转):应用本身程序不负责依赖对象的创建及维护,依赖对象的创建及维护有外设容器负责,即:IOC: DI(依赖注 ...
- spring笔记(二)
共性问题: 1. 服务器启动报错,什么原因? * jar包缺少.jar包冲突 1) 先检查项目中是否缺少jar包引用 2) 服务器: 检查jar包有没有发布到服务器下: 用户库jar包,需要手动发布到 ...
- Spring 笔记 -06- 从 MySQL 建库到 登录验证数据库信息(maven)
Spring 笔记 -06- 从 MySQL 建库到 登录验证数据库信息(maven) 本篇和 Spring 没有什么关系,只是学习 Spring,必备一些知识,所以放在这里了. 本篇内容: (1)M ...
- JDBC学习笔记二
JDBC学习笔记二 4.execute()方法执行SQL语句 execute几乎可以执行任何SQL语句,当execute执行过SQL语句之后会返回一个布尔类型的值,代表是否返回了ResultSet对象 ...
- MyBatis笔记二:配置
MyBatis笔记二:配置 1.全局配置 1.properites 这个配置主要是引入我们的 properites 配置文件的: <properties resource="db.pr ...
- Spring笔记(6) - Spring的BeanFactoryPostProcessor探究
一.背景 在说BeanFactoryPostProcessor之前,先来说下BeanPostProcessor,在前文Spring笔记(2) - 生命周期/属性赋值/自动装配及部分源码解析中讲解了Be ...
- 《CMake实践》笔记二:INSTALL/CMAKE_INSTALL_PREFIX
<CMake实践>笔记一:PROJECT/MESSAGE/ADD_EXECUTABLE <CMake实践>笔记二:INSTALL/CMAKE_INSTALL_PREFIX &l ...
- jQuery源码笔记(二):定义了一些变量和函数 jQuery = function(){}
笔记(二)也分为三部分: 一. 介绍: 注释说明:v2.0.3版本.Sizzle选择器.MIT软件许可注释中的#的信息索引.查询地址(英文版)匿名函数自执行:window参数及undefined参数意 ...
- Mastering Web Application Development with AngularJS 读书笔记(二)
第一章笔记 (二) 一.scopes的层级和事件系统(the eventing system) 在层级中管理的scopes可以被用做事件总线.AngularJS 允许我们去传播已经命名的事件用一种有效 ...
随机推荐
- sublime text 3 配置terminal 启动npm
前期工作.安装node.js 配置全家变量 1:ctrl + shift + p 打开 输入intall package 回车 2:安装node.js 插件 3:再安装terminal 4:Prefe ...
- 编程 - 前端 - JavaScript - 库 - ECharts (开源可视化)
ECharts,一个使用 JavaScript 实现的开源可视化库,可以流畅的运行在 PC 和移动设备上,兼容当前绝大部分浏览器(IE8/9/10/11,Chrome,Firefox,Safari等) ...
- html5 手风琴菜单
因为项目需要,现在需要做个手风琴菜单,于是自己就瞎整了一下,所用只是less.js javascript jquery效果如图: 具体代码如下: <!DOCTYPE html> < ...
- MySQL实现排名并查询指定用户排名功能,并列排名功能
MySQL实现排名并查询指定用户排名功能,并列排名功能 表结构: CREATE TABLE test.testsort ( id int(11) NOT NULL AUTO_INCREMENT, ui ...
- Asp.Net Core 生成图形验证码
前几天有朋友问我怎么生成图片验证码,话不多说直接上代码. 支持.NET CORE开源.助力.NET Core社区发展. using System; using System.IO; using Sys ...
- 国内maven库链接地址,链接阿里的库,下载很快!!!
<mirror> <id>alimaven</id> <name>aliyun maven</name> <url>http:/ ...
- django开发傻瓜教程-3-celery异步处理
Ref: https://www.jianshu.com/p/6f8576a37a3e https://blog.csdn.net/Demo_3/article/details/78119951 ht ...
- atoi 和 atof (把数字字符串转化为数字储存)
int atoi(char *s) 如果字符串内容是整数就返回该整数,否则返回0 double atof(char *s) 同上,不过返回浮点型 #include<iostream> #i ...
- Python入门及容易!网摘分享给大家!
Python:Python学习总结 背景 PHP的$和->让人输入的手疼(PHP确实非常简洁和强大,适合WEB编程),Ruby的#.@.@@也好不到哪里(OO人员最该学习的一门语言). Pyth ...
- mysql学习第四天(高级查询)
-- 第七章-- 1.查询入职日期最早和最晚的日期select min(hiredate),max(hiredate)from emp -- 2.查询职位以SALES开头的所有员工平均工资,最低工资, ...