初次接入Spring-001

1.什么是框架?

 框架是一种重复使用的解决方案,针对某个软件开发的问题提出的。

 Spring框架,它是一个大型的包含很多重复使用的某个领域的解决方案。

 Spring的理念:不要重复发明轮子。

 

 

 

2.Spring的理解?

 首先,Spring是一个容器。它是装对象的。主要就是通过搜索class的路径。找出bean对象,实际就是根据反射来获取这个bean对象的:

Class<?> classit=Class.forName("com.jinglin.model.Person");

 Person p =(Person)classit.newInstance();

3.IOC的理解?

 控制反转的容器。

 把对象的获取的主动权交出去

4.DI:

 依赖注入,通过在构建bean对象的时候,把数据加入到对象中,所谓的依赖的意思就是这里的数据有可能是通过其它的bean对象得到的。

5.对于Spring中的bean的注入方式

1set方式注入,注入数据的前提是必须要有一个set方法,在bean的类里。是通过属性的注入方式:

java类:

package com.jinglin.model;

 

import java.util.List;

import java.util.Map;

import java.util.Set;

 

public class Boy {

private String name;

private Girl girlfriend;

public String getName() {

return name;

}

 

public void setName(String name) {

this.name = name;

}

 

public Girl getGirlfriend() {

return girlfriend;

}

 

public void setGirlfriend(Girl girlfriend) {

this.girlfriend = girlfriend;

}

private List<String> habbies;//爱好

private Map<String,Float> subjects;

public Map<String, Float> getSubjects() {

return subjects;

}

 

public void setSubjects(Map<String, Float> subjects) {

this.subjects = subjects;

}

 

public List<String> getHabbies() {

return habbies;

}

 

public void setHabbies(List<String> habbies) {

this.habbies = habbies;

}

//如果这个boy的女朋友不止一个

private Set<Girl> setgirlfriends;

public Set<Girl> getSetgirlfriends() {

return setgirlfriends;

}

public void setSetgirlfriends(Set<Girl> setgirlfriends) {

this.setgirlfriends = setgirlfriends;

}

}

2)构造函数的注入,初始化对象的时候通过构造函数传入数据:

public Person(String _name,Integer _age){

this.name=_name;

this.age=_age;

}

6.那么由spring给我们提供的bean对象的作用域?

 1)默认情况下,spring提供的bean对象是共享模式的。

在内存中只出现一个实例化对象。

默认的bean的作用域:scope="singleton"

2)如果更改bean的作用域,就是非共享模式,

scope="prototype"

 

7.自动装配+注解,简化spring中的bean对象的开发。

 Spring中的bean对象自动从容器里搜索和自己的属性字段名一致的情况,如果有,就自动匹配。

 开发步骤,首先明确,要将所有的包扫描的spring容器里。

 1)在applicationContext.xml里做springbeans的声明。  

<beans

    xmlns="http://www.springframework.org/schema/beans"   

    xmlns:p="http://www.springframework.org/schema/p"   

    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.0.xsd   

    http://www.springframework.org/schema/context

    http://www.springframework.org/schema/context/spring-context.xsd">

2)将各个包扫描到spring里。

<!-- 将包扫描到spring组件里 -->

    <context:component-scan base-package="com.jinglin.dao">

    </context:component-scan>

    <context:component-scan base-package="com.jinglin.service">

    </context:component-scan>

3)给每个类加入注解,告诉spring。将其加入到spring的容器里。

@Component("goodsInfoDao")

public class GoodsInfoDao {

   public void insertitem(){

   System.out.println("这是商品信息的数据插入");

   }

}

4)为了简化配置,采取的自动装配。一般都是通过名字自动装配的:

<beans

    xmlns="http://www.springframework.org/schema/beans"   

    xmlns:p="http://www.springframework.org/schema/p"   

    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.0.xsd   

    http://www.springframework.org/schema/context

    http://www.springframework.org/schema/context/spring-context.xsd"

    default-autowire="byName">

5)在开发的时候,对于同spring中相同的beanid,那么采取自动装配:

@Component("userInfoService")

public class UserInfoService {

@Autowired

    private UserInfoDao userInfoDao;

 

public void additem(){

System.out.println("这是用户调用的业务层");

userInfoDao.insertitem();

}

}

8.Spring的延迟加载,当我们需要这个spring提供的bean对象的时候,这个时候spring才会去生成这个对象(默认情况下,当spring加载它的文件的时候,就全部将spring里配置的bean对象生成)。

 1)在applicationContext配置。

<beans

    xmlns="http://www.springframework.org/schema/beans"   

    xmlns:p="http://www.springframework.org/schema/p"   

    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.0.xsd   

    http://www.springframework.org/schema/context

    http://www.springframework.org/schema/context/spring-context.xsd"

    default-autowire="byName"

    default-lazy-init="true">

缺点:无法对单个的bean对象提供延迟加载配置。

2)针对单个的bean对象:

<bean id="person" lazy-init="true"  class="com.jinglin.model.Person">

        <!-- 通过构造参数的方式注入值 -->

        <constructor-arg value="zhangsan"></constructor-arg>

        <constructor-arg value="14"></constructor-arg>

    </bean>

3)直接在类中加注解,表示就是延迟加载。

@Component("goodsInfoService")

@Lazy

public class GoodsInfoService {

@Autowired

private GoodsInfoDao goodsInfoDao;

public void additem(){

System.out.println("这是商品信息的业务处理方法");

   goodsInfoDao.insertitem();

}

public GoodsInfoService(){

System.out.println("这是goodsInfoService的无参构造函数");

}

   

}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值