当前位置:   article > 正文

框架学习第一天(spring)_框架你的一天

框架你的一天

一、Spring

(一)spring介绍

(1)定义 spring框架是一个企业级的轻量级框架,低倾入的框架(也是在后期项目维护,修改代码量比较少)

(2)优势

1、其实spring就是一个大工厂(工厂者设计模式)(生产管理实体bean,不用实例化对象,所有的都是通过spring容器进行管理)
2、支持分布式事务
3、支持测试,与jutext 测试有良好的结合	
4、与其他的框架的结合度比较好(ssh,ssm)	
5、支持企业级的api的开发(邮箱,任务调度)
  • 1
  • 2
  • 3
  • 4
  • 5

(二)模块介绍

(1)spring各个模块:

  1. bean ,主要用来管理和产生对象;
  2. core,主要用来提供一些核心基础服务;
  3. context ,提供api开发;
  4. 模块分布图:spring各个模块的介绍## (2)搭建spring框架
  5. 导入核心jar包
  6. 导入核心配置文件(applicationContext.xml)
  7. 测试

(3)Idea软件快捷键

  1. 获取setter,getter方法:Alt + Insert
  2. 强转:Alt + Enter
  3. 复制代码:Ctrl/Command+ D
  4. 整理代码:Alt + Shift + L

二、控制反转(ioc)

控制反转(就是不需要自己来实例化这个对象,而依赖于我们容器,也就是spring框架)
  • 1
public class Boy  implements Serializable {
//说的方法
	public  void  say(){
        	System.out.println("正在说话!");
	}
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
<bean id="boy" class="com.offcn.entity.Boy"></bean>
  • 1
  • 1
ApplicationContext app = new ClassPathXmlApplicationContext("appliactionContext.xml");
//得到具体的实体bean,这一个参数传递的就是你的配置文件id
Boy boy = (Boy) app.getBean("boy");
boy.say();
  • 1
  • 2
  • 3
  • 4
  • 1
  • 2
  • 3
  • 4

三、依赖注入(动态的来实例化对象,用到的时候,来实例化对象)

(一)动态来生产某个对象的步骤

(1)、实现接口的方法

  1. 给其set方法,配置引入对象:<property name="boy" ref="boy"></property>
  2. 加载配置文件
    第一种
    	ApplicationContext app = new ClassPathXmlApplicationContext("appliactionContext.xml");
    	//得到具体的实体bean,这一个参数传递的就是你的配置文件id
    	Boy boy = (Boy) app.getBean("boy");
    	boy.say();
    
    • 1
    • 2
    • 3
    • 4
    • 1
    • 2
    • 3
    • 4
    第二种
    	ApplicationContext app = new ClassPathXmlApplicationContext("appliactionContext.xml");
    	Girl girl = (Girl) app.getBean("girl");
    	girl.kiss();
    
    • 1
    • 2
    • 3
    • 1
    • 2
    • 3
    第三种
    	BeanFactory app = new XmlBeanFactory(new FileSystemResource("D:\\workspace\\springDay01\\src\\appliactionContext.xml"));
    	UserServiceImpl userService = (UserServiceImpl) app.getBean("userService");
    	userService.add();
    
    • 1
    • 2
    • 3
    • 1
    • 2
    • 3

(2):使用工厂的方式

  1. 第一种:静态工厂
    	//用静态工厂的方式来产生对象
    	public static UserService createUserService(){
    			return new UserServiceImpl();
    	}
    
    • 1
    • 2
    • 3
    • 4
    • 1
    • 2
    • 3
    • 4
    	<bean id="userObject" class="com.offcn.utils.UserObject" factory-method="createUserService"></bean>
    
    • 1
    • 1
  2. 第二种:非静态工厂
    	//使用非静态来实例化对象
    	public  UserService create1UserService(){
    			return new UserServiceImpl();
    	}
    
    • 1
    • 2
    • 3
    • 4
    • 1
    • 2
    • 3
    • 4
    	//使用非静态的方式来实例化对象
    	//factory-bean代表你要实例化对象的id factory-method引入idbean里面的方法
    	<bean id="userObject" class="com.offcn.utils.UserObject"></bean>
    	<bean id="user" factory-bean="userObject" factory-method="create1UserService"></bean>
    
    • 1
    • 2
    • 3
    • 4
    • 1
    • 2
    • 3
    • 4

(二)用appliactionContext对各种对象进行赋值

  1. 给属性赋值
    private  int sid;
    private  String sname;
    //调用set方法
    public void setSid(int sid) {
    		this.sid = sid;
    }
    
    public void setSname(String sname) {
    	this.sname = sname;
    }
    public  void  print(){
    	System.out.println(this.sid+"\t"+this.sname);
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    <bean id="userObject" class="com.offcn.utils.UserObject">
    	<property name="sid" value="11"/>
    	<property name="sname" value="asd"/>
    </bean>
    
    • 1
    • 2
    • 3
    • 4
    • 1
    • 2
    • 3
    • 4
  2. 给对象赋值
    private Person person;
    public void setPerson(Person person) {
    	this.person = person;
    }
    public  void  print(){
    System.out.println(this.person.getPid()+"\t"+this.person.getPname());
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    //<bean id="person" class="com.offcn.entity.Person" p:pid="12" p:pname="asdzxc"></bean>
    <bean id="userObject" class="com.offcn.utils.UserObject">
    	<property name="person" ref="person"/>
    </bean>
    
    • 1
    • 2
    • 3
    • 4
    • 1
    • 2
    • 3
    • 4
  3. 给list集合进行值注入
    //给list集合进行值注入
    private List<Object> list;
    public void setList(List<Object> list) {
       	this.list = list;
    }
    public  void  print(){
       if (list != null && list.size()>0){
       	for (Object o:list) {
       		System.out.println(o);
       	}
       }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    <bean id="userObject" class="com.offcn.utils.UserObject">
    //装配list集合
    <property name="list">
       <list>
      	<value>admin</value>
       	<value>lisi</value>
       	<value>wangwu</value>
       	<value>lida</value>
       </list>
    </property>
    </bean>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
  4. 给set集合进行值注入
    //给set集合进行值注入
    private Set<Object> set;
    public void setSet(Set<Object> set) {
       this.set = set;
    }
    public  void  print(){
       //遍历set集合
       Iterator<Object>iterator = set.iterator();
       while (iterator.hasNext()){
       Object object = iterator.next();
       System.out.println(object);
       }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    <bean id="userObject" class="com.offcn.utils.UserObject">
    //装配set集合
    <property name="set">
    	<set>
        	<value>asd</value>
        	<value>asd1</value>
        	<value>asd2</value>
        	<value>asd3</value>
    	</set>
    </property>
    </bean>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
  5. 给map集合进行值注入
    //给map集合进行值注入
    private Map<String,String> map;
    public void setMap(Map<String, String> map) {
    this.map = map;
    }
    public  void  print(){
    //遍历map集合
       Set<String> set1 = map.keySet();
       Iterator<String>iterator2 = set1.iterator();
       while (iterator2.hasNext()){
       	Object object = iterator2.next();
       	System.out.println(object+"\t"+map.get(object));
       }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    <bean id="userObject" class="com.offcn.utils.UserObject">
    //装配map集合
    <property name="map">
    	<map>
        	<entry key="01">
            <value>编号01</value>
        </entry>
        <entry key="02">
            <value>编号02</value>
        </entry>
    	</map>
    </property>
    </bean>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
  6. 给数组赋值
    //给数组赋值
    private  Object[] objects;
    public void setObjects(Object[] objects) {
    	this.objects = objects;
    }
    public  void  print(){
    //遍历数组
    if (objects != null && objects.length>0){
    		for (int i=0;i<objects.length;i++){
       		 	System.out.println(objects[i]);
    		}
    	}
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    <bean id="userObject" class="com.offcn.utils.UserObject">
    //装配数组集合
    <property name="objects">
     <array>
        	<value></value>
        	<value></value>
        	<value></value>
        	<value></value>
    	</array>
    </property>
    </bean>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11

四、三种实例化bean的方式(使用P命名空间来进行赋值[1])

第一种:使用接口和实现类(第三点中,实现接口的方法);
第二种:静态工厂模式又叫简单工厂模式(第三点中,使用工厂的方式的第一种);
第三种:非静态工厂又叫工厂方法模式(第三点中,使用工厂的方式的第二种);
  • 1
  • 2
  • 3

五、依赖注入之装配方法1

(一)手动装配

(1)setter

详解请参考 第三点中的用appliactionContext对各种对象进行赋值
  • 1

(2)构造函数

public class Student implements Serializable {
    //用构造方法来进行值注入
    private int sid;
    private String sname;
    private String spwd;

    public Student(int sid, String sname, String spwd) {
        this.sid = sid;
        this.sname = sname;
        this.spwd = spwd;
    }

    public Student(int sid, String sname) {
        this.sid = sid;
        this.sname = sname;
    }

    public Student(String sname, String spwd) {
        this.sname = sname;
        this.spwd = spwd;
    }
    //输出结果
    public void print(){
        System.out.println(this.sid+"\t"+this.sname+"\t"+this.spwd);
    }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
<bean id="student" class="com.offcn.entity.Student">
   //用构造进行值注入 1.通过索引,起始为0
   <constructor-arg index="0" value="11"></constructor-arg>
   <constructor-arg index="1" value="12"></constructor-arg>
   <constructor-arg index="2" value="13"></constructor-arg>
</bean>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
<bean id="student" class="com.offcn.entity.Student">
    //用构造进行值注入 1.通过索引,起始为0
    <constructor-arg index="0" value="11"></constructor-arg>
    <constructor-arg index="1" value="12"></constructor-arg>
</bean>
//词运行的结果为:0 11 12
//其中 0 为默认索引值
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
<bean id="student" class="com.offcn.entity.Student">
    //用构造进行值注入 1.通过索引,起始为0
    <constructor-arg name="sid" value="11"></constructor-arg>
    <constructor-arg name="sname" value="12"></constructor-arg>
</bean>
//此方法为常用方法
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6

(3)注解

public interface UserDao {
    //增加的方法
    public void add();
}
  • 1
  • 2
  • 3
  • 4
  • 1
  • 2
  • 3
  • 4
//dao层注解 固定的
@Repository("userDao")
public class UserDaoImpl implements UserDao {
    @Override
    public void add() {
        System.out.println("增加成功了");
    }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
public interface UserService {
    public void add();
}
  • 1
  • 2
  • 3
  • 1
  • 2
  • 3
//第一种
@Service("userService")
public class UserServiceImpl implements UserService {
    //service 要注入dao对象
    //代表自动装备,按照类型进行装备
    @Autowired
    //按照名称进行装备
    @Qualifier("userDao")
    //用Java 的注解进行装备;jdk 的版本必须在1.6以上
    //@Resource
    private UserDao user;
    @Override
    public void add() {
        user.add();
    }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
//第二种
//用set方法进行装备
@Service("userService")
public class UserServiceImpl implements UserService {
   @Autowired
   private UserDao user
   public void setUserDao(@Qualifier("userDao") UserDao userDao){
        this.user = userDao;
    }
    @Override
    public void add() {
        user.add();
    }
}
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
//第三种(常用方法)
@Service("userService")
public class UserServiceImpl implements UserService {
    //service 要注入dao对象
     //用Java 的注解进行装备;jdk 的版本必须在1.6以上
    @Resource
    private UserDao user;
    @Override
    public void add() {
        user.add();
    }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
public class test {
    public static void main(String[] args) {
        ApplicationContext app = new ClassPathXmlApplicationContext("applicationContext.xml");
        UserService userService = (UserService) app.getBean("userService");
        userService.add();
    }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
//导入这个文件`applicationContext.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-3.2.xsd
   http://www.springframework.org/schema/context 
   http://www.springframework.org/schema/context/spring-context-3.2.xsd">
   <!-- 扫描包中注解标注的类 -->
   <context:component-scan base-package="com.offcn.service,com.offcn.dao" />
</beans>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12

Tomcat配置

  1. 第一步第一步
  2. 第二步第二步
    找到它
  3. 第三步
    第三步
    应用
  4. 第四步
    添加tomcat
    在这里插入图片描述
  5. 第五步
    第五步
    [1]: 见本文第三点的第二点中,给对象赋值的注释

  1. jdk 的版本必须在1.6以上 ↩︎

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/AllinToyou/article/detail/454404
推荐阅读
相关标签
  

闽ICP备14008679号