1.IOC/DI(控制反转/依赖注入)
IOC是什么?IOC完成的事情是原先程序员主动通过new实例化对象的事情,转交给spring负责
IOC最大的作用是:解耦,程序员不需要管理对象,解除了对象管理和程序员之间的耦合
2.AOP(面向切面编程)
3.声明式事务
Spring 框架采用分层架构,根据不同的功能被划分成了多个模块,这些模块大体可分为 Data Access/Integration、Web、AOP、Aspects、Messaging、Instrumentation、Core Container 和 Test(见上图)
详细见:http://c.biancheng.net/view/4242.html
1.通过构造方法创建
1.1 在applicationContext.xml中设置哪个构造方法创建对象
1.2 如果设定的条件匹配多个构造方法,则执行最后的构造方法
1.3 index:参数的索引,从0开始,name:参数名,type:类型(区分关键字和封装类,例 int和integer)
2.实例工厂
2.1 工厂设计模式:帮助创建类对象,一个工厂可以生产多个对象
2.2 实例工厂:需要先创建工厂,才能生产对象,然后在xml中配置工厂对象和需要创建的对象
package com.spring.po;
public class PeopleFactory {
public People newInstance(){
return new People(3,"实例工厂");
}
}
<!-- 实例工厂实现实例 -->
<bean id="factory" class="com.spring.po.PeopleFactory"></bean>
<bean id="pro3" factory-bean="factory" factory-method="newInstance"></bean>
3.静态工厂:编写一个静态工厂,在方法上添加static
package com.spring.po;
public class PeopleStaticFactory {
public static People newInstance(){
return new People(4, "静态");
}
}
IDE:myeclipse2017
jar包:spring4个基础jar包(本案例使用5.2版本)+Commons-logging的jar包
新建项目spring01,项目结构图如下
applicationContext.xml
<!-- 实例工厂实现实例 -->
<bean id="factory" class="com.spring.po.PeopleFactory"></bean>
<bean id="pro3" factory-bean="factory" factory-method="newInstance"></bean>
<!-- 静态工厂实例实例 -->
<bean id="pro4" class="com.spring.po.PeopleStaticFactory" factory-method="newInstance"></bean>
com.spring.po包
People.java
package com.spring.po;
public class People {
private int id;
private String name;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@Override
public String toString() {
return "People [id=" + id + ", name=" + name + "]";
}
public People() {
super();
}
public People(int id,String name){
this.id=id;
this.name=name;
System.out.println("有参构造函数");
}
}
PeopleFactory.java
package com.spring.po;
public class PeopleFactory {
public People newInstance(){
return new People(3,"实例工厂");
}
}
PeopleStaticFactory.java
package com.spring.po;
public class PeopleStaticFactory {
public static People newInstance(){
return new People(4, "静态");
}
}
com.spring.test包
Test.java
package com.spring.test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.spring.po.People;
public class Test {
public static void main(String[] args) {
ApplicationContext ac=new ClassPathXmlApplicationContext("applicationContext.xml");
People people= ac.getBean("pro",People.class);
people.setId(1);
people.setName("测试");
People people2=ac.getBean("pro2",People.class);
People people3=ac.getBean("pro3",People.class);
People people4=ac.getBean("pro4",People.class);
System.out.println(people);
System.out.println(people2);
System.out.println(people3);
System.out.println(people4);
}
}
运行结果如下
IDE:myeclipse2017
数据库的airport表数据
项目具体代码在后面
1.导入mybatis所有jar包和spring基本jar包,spring-idbc,spring-tx,spring-aop,spring-web
2.编写spring配置文件applicationContext.xml
3.编写代码
3.1 正常编写po(实体类)
3.2 编写Mapper包下时必须使用接口绑定方案或注解方案(必须有接口)
3.3 正常编写service接口和service实现类(impl)
3.3.1 需要在service实现类中声明Mapper接口对象,并生成get/set方法
3.4 spring无法管理servlet
结构图
applicationContext.xml
com.spring.po包
Airport.java
package com.spring.po;
public class Airport {
private int id;
private String portName;
private String cityName;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getPortName() {
return portName;
}
public void setPortName(String portName) {
this.portName = portName;
}
public String getCityName() {
return cityName;
}
public void setCityName(String cityName) {
this.cityName = cityName;
}
@Override
public String toString() {
return "Airport \[id=" + id + ", portName=" + portName + ", cityName=" + cityName + "\]";
}
}
com.spring.mapper包
AirportMapper.java
package com.spring.mapper;
import java.util.List;
import org.apache.ibatis.annotations.Select;
import com.spring.po.Airport;
public interface AirportMapper {
@Select("select * from airport")
List
}
com.spring.service包
AirportService.java
package com.spring.service;
import java.util.List;
import com.spring.po.Airport;
public interface AirportService {
List
}
com.spring.service.impl包
AirportServiceImpl.java
package com.spring.service.impl;
import java.util.List;
import com.spring.mapper.AirportMapper;
import com.spring.po.Airport;
import com.spring.service.AirportService;
public class AirportServiceImpl implements AirportService{
private AirportMapper airportMapper;
public AirportMapper getAirportMapper() {
return airportMapper;
}
public void setAirportMapper(AirportMapper airportMapper) {
this.airportMapper = airportMapper;
}
@Override
public List<Airport> show() {
return airportMapper.selAll();
}
}
com.spring.test包
Test.java
可以测试运行test
package com.spring.test;
import java.util.List;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.spring.po.Airport;
import com.spring.service.impl.AirportServiceImpl;
public class Test {
public static void main(String[] args) {
ApplicationContext ac=new ClassPathXmlApplicationContext("applicationContext.xml");
// String [] names=ac.getBeanDefinitionNames();
// for (String string : names) {
// System.out.println(string);
// }
AirportServiceImpl bean= ac.getBean("airportService",AirportServiceImpl.class);
List<Airport> list=bean.show();
// System.out.println(list);
for (Airport airport : list) {
System.out.println(airport);
}
}
}
com.spring.servlet包
AirportServlet.java
package com.spring.servlet;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;
import com.spring.service.AirportService;
import com.spring.service.impl.AirportServiceImpl;
@WebServlet("/airport")
public class AirportServlet extends HttpServlet {
private AirportService airportService;
@Override
public void init() throws ServletException {
//对service实例化
WebApplicationContext ac=WebApplicationContextUtils.getRequiredWebApplicationContext(getServletContext());
airportService=ac.getBean("airportService",AirportServiceImpl.class);
}
@Override
protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
req.setAttribute("list", airportService.show());
req.getRequestDispatcher("index.jsp").forward(req, resp);
}
}
web.xml
index.jsp
<%@ page language="java" import="java.util.\*" pageEncoding="utf-8"%> <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
运行
1.在src下新建xxx.properties文件
2.在spring配置文件中引入xmlns:context,在下面添加,如果需要加载多个配置文件,用逗号分隔
3.添加了属性文件,并且在
3.1 SqlSessionFactoryBean的id不能叫做sqlSessionFactory
3.2 修改扫描器(MapperScannerConfigurer),把原来ref引用替换value赋值,自动注入只能影响ref,不会影响value赋值
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<!-- 要扫描的包 -->
<property name="basePackage" value="com.spring.mapper"></property>
<!-- 和Factory产生关系 -->
<property name="sqlSessionFactoryBeanName" value="factory"></property>
</bean>
4.在被spring管理的类通过@Value("${key}")取出properties中的内容
4.1添加注解扫描
4.2在类中添加
key和变量名可以不相同
变量类型任意,只要保证key对应的value能转换成这个类型就可以
在上个项目中src下添加db.properties和second.properties
second.properties
my.demo=hello
my.demo1=123
db.properties
jdbc.driverClassName=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/test?serverTimezone=UTC
jdbc.username=root
jdbc.pwd=123456
修改UsersServiceImpl.java
package com.spring.service.impl;
import org.springframework.beans.factory.annotation.Value;
import com.spring.mapper.UsersMapper;
import com.spring.po.Users;
import com.spring.service.UsersService;
public class UsersServiceImpl implements UsersService {
@Value("${my.demo}")
private String test;
@Value("${my.demo1}")
private String test1;
private UsersMapper usersMapper;
public UsersMapper getUsersMapper() {
return usersMapper;
}
public void setUsersMapper(UsersMapper usersMapper) {
this.usersMapper = usersMapper;
}
@Override
public Users login(Users users) {
System.out.println("输出:"+test+" "+test1);
return usersMapper.selByUsers(users);
}
}
修改applicationContext.xml
<bean id="mybefore" class="com.spring.advice.MyBefore"></bean>
<bean id="myAfter" class="com.spring.advice.MyAfter"></bean>
<!-- aop -->
<aop:aspectj-autoproxy proxy-target-class="true"></aop:aspectj-autoproxy>
<aop:config>
<aop:pointcut expression="execution(\* com.spring.service.impl.UsersServiceImpl.login(..))" id="mypoint"/>
<aop:advisor advice-ref="mybefore" pointcut-ref="mypoint"/>
<aop:advisor advice-ref="myAfter" pointcut-ref="mypoint"/>
</aop:config>
运行登录,控制台结果如下
1.
2.作用:控制对象有效范围(单例,多例)
3.
4.scope可取值
singleton 默认值,单例
prototype 多例,每次获取重新实例化
request 每次请求重新实例化
session 每次会话对象内,对象内是单例的
application 在application对象内是单例的
参考https://www.cnblogs.com/dj66194/p/7654555.html,感谢“还有梦”
1.@Component:创建类对象,相当于配置
2.@Service:与Component功能相同,写在serviceImpl类上
3.@Repository:与Component功能相同,写在数据访问层类上
4.@Controller:与Component功能相同,写在控制器类上
5.@Resource:不需要写对象的get/set,java中的注解,默认按照byName注入,如果没有名称对象,则按照byType注入,建议把对象名称和spring容器中对象名相同
6.@Autowired:不需要写对象的get/set,spring注解,默认按照byType注入
7.@Value():获取properties文件内容
8.@Pointcut():定义切点
9.@ASpect():定义切面类
10.@Before():前置通知
11.@Alter:后置通知
12.@AlterReturning:后置通知,切点须正确执行
13.@AlterThrowing:异常通知
14.@Around:环绕通知
手机扫一扫
移动阅读更方便
你可能感兴趣的文章