美文网首页
2018-11-18

2018-11-18

作者: 南诏国总统 | 来源:发表于2018-11-18 21:11 被阅读0次

E=mc^2

6. 高效绘制 甘特图

    title 项目开发流程
    section 项目确定
        需求分析       :a1, 2016-06-22, 3d
        可行性报告     :after a1, 5d
        概念验证       : 5d
    section 项目实施
        概要设计      :2016-07-05  , 5d
        详细设计      :2016-07-08, 10d
        编码          :2016-07-15, 10d
        测试          :2016-07-22, 5d
    section 发布验收
        发布: 2d
        验收: 3d

【回顾】

1.bean的作用域

<bean id="" class="" scope="singleton/prototype"/>

1.1java

    public String checkUserName(String name) {
    //模拟逻辑
    
    if("Tom".equals(name)) {
        
        return "已经注册";
        }else {
            new Message(0,"已经使用");
            return "可以注册";
        }
        
    }

2.延迟加载

默认是立即加载:
<bean id="" class="" lazy-init="true"/>

3.IOC:(重点)

4.DI:依赖注入 方式:构造方法,set方法

5.set方法依赖(推荐使用)

<bean id="" class="">
    <property name="属性名" ref="id名"/>
</bean>

6.构造方法依赖注入

<bean id="" class="">
    <constructor-arg index="索引值" ref="id值"/>
</bean>

7.自动装配(了解)

<bean id="" class="" autowire="byName/byType"/>

8.各种属性值的依赖注入

基本数据类型和字符串

<bean
    <property name="" value=""/>
</bean>

list set map Properties

<list>  <set> <map> <props>

使用集合对象给变量赋值

     <!-- 实例化一个list对象 -->
 <util:list id="list2">
     <value>旅游</value>
     <value>电视剧</value>
     <value>听音乐</value>
 </util:list>
 <!-- 实例化一个set对象 -->
 <util:set id="set2">
    <value>沈阳市</value>
    <value>大连市</value>
    <value>鞍山市</value>
 </util:set>
 <!-- 
    使用容器创建好的集合对象给变量赋值
  -->
 <bean id="valueBean2" 
       class="cn.tedu.spring.bean.ValueBean">
      <property name="like" ref="list2"/>
      <property name="city" ref="set2"/>
 </bean>

解析外部的配置文件

在resources文件夹下,新建db.properties(和数据库连接相关的信息)

driverClassName=com.mysql.jdbc.Driver
url=jdbc:mysql://localhost:3306/db
username=root
password=root

开发步骤
1)创建maven工程
    添加web.xml
    添加tomcat运行环境
    添加jar spring-webmvc,junit,commons-dbcp,mysql
    添加application.xml

2)配置数据库 的连接池信息

    <!-- 
    1.util:properties表示读取外部的属性文件,并实例化对象
    2.id表示名称
    3.location表示属性文件的位置
   -->
  <util:properties id="dbConf" 
                  location="classpath:db.properties"/>
  
  <!-- 配置数据库的连接池 
        1.使用spring表达式给属性赋值 
        2.spring表达式语法格式:#{}
  -->
  <bean id="dataSource" 
  class="org.apache.commons.dbcp.BasicDataSource">
    <property name="driverClassName" 
              value="#{dbConf.driverClassName}"/>
    <property name="url" 
              value="#{dbConf.url}"/>
    <property name="username" 
              value="#{dbConf.username}"/>
    <property name="password" 
              value="#{dbConf.password}"/>
  </bean>

基于注解的bean管理

1.基于注解的方式实例化对象(推荐使用)

实例化对象和依赖注入有两种实现方式:配置文件,注解

1.扫描包

<!-- 扫描包
      可以扫描到当前包和子包下的所有类
   -->
  <context:component-scan 
            base-package="cn.tedu.dao"/>

2.特定功能(实例化功能)的注解

@Component:通用的注解:实例化对象
@Controller:实例化控制器类的对象
@Service:实例化业务层类的对象
@Repository:实例化持久层类的对象


/*
 * 1.@Repository表示实例化持久层的注解
 * 2.id名默认类名第一个字母小写
 * 3.id名可以自定义:@Repository("userDao")
 */
@Repository("userDao")
public class UserDaoImpl implements UserDao{

public void insertUser() {
    System.out.println("添加成功!");
    
}

}

2.生命周期管理

//@Component表示实例化对象
@Component
public class BeanLife {
public BeanLife(){
    System.out.println("BeanLife");
}
//@PostConstruct表示定义初始化方法
//@PostConstruct:Tomcat运行环境依赖的jar包
@PostConstruct
public void init(){
    System.out.println("init");
}
public void execute(){
    System.out.println("execute");
}
//@PreDestroy表示定义销毁的方法
//@PreDestroy:Tomcat运行环境依赖的jar包
@PreDestroy
public void destroy(){
    System.out.println("destroy");
}

}

3.bean作用域

//通过注解实例化对象,默认为单例singleton
//@Scope定义bean的作用域
//@Scope("prototype")表示bean作用域为多例
@Component
@Scope("prototype")
public class DemoScope {

}

4.延迟加载

//默认bean对象的实例化,是立即加载
//@Lazy设置bean是否延迟加载的注解
//@Lazy(true)设置bean对象为延迟加载
@Component
@Lazy(true)
public class DemoLazy {
    public DemoLazy(){
        System.out.println("DemoLazy");
    }

}

DI

1.@Resource(推荐使用)

//1.@Resource  tomcat运行环境依赖jar包中定义的注解
//2.@Resource 实现依赖注入
//3.@Resource 实现依赖注入,可以省略set方法
//4.@Resource 默认依赖注入的方式为byName
//5.@Resource 如果没有匹配的属性,
//            那么按照byType方式实现依赖注入
//6.@Resource(name="userDaoImpl")
//          表示指定id为userDaoImpl名字的对象依赖注入
@Resource(name="userDaoImpl")
private UserDao userDao;

2.@Autowired(了解)

ublic class UserServiceImpl2 implements UserService2{
//1.@Autowired 依赖注入
//2.@Autowired 默认依赖注入的方式byType;
//             如果有多个相同类型的对象,那么按照byName依赖注入
//3.如果使用byName实现依赖注入,
//  使用@Qualifier注解定义匹配的名称
//4.@Qualifier不能单独使用
@Autowired
@Qualifier("userDaoImpl")
private UserDao userDao;

public void addUser() {
    userDao.insertUser();
    
}

}

3.@Value(了解)

@Component
public class Student {
    //@Value("Admin")给String或者基本数据类型依赖注入
    //@Value("#{conf.name}")使用spring表达式实现依赖注入
    @Value("#{conf.name}")
    private String name;
    
    public String toString(){
        return "name="+name;
    }
    
}

小结:注解

1.实例化对象和依赖注入有两种实现方式:配置文件,注解。

2.实例化对象的注解

@Component:通用注解
@Controller:实例化控制器对象
@Service:实例化业务层对象
@Repository:实例化持久层对象

3.依赖注入的注解

@Resource:默认按属性名依赖注入,如果匹配不上,通过类型匹配依赖注入
@Resource(name="id名")

4.了解

@PostConstruct
@PreDestroy
@Scope
@Lazy
@Autowired
@Qualifier  

练习:

abstract Animal{
    abstract void showInfo();
}
Cat Dog 分别继承Animal               要求:

public class Cat extends Animal{      1.分别实例化3个对象
    private String name;              2.分别给cat,dog起名
    public void showInfo(){           3.Person类中的a为Cat
        .......(name);                4.定义测试类,调用handler
    }                                   方法
}
Dog同上
public class Person{
    private Animal a;
    public void handler(){
        a.showInfo();
    }
}

--

相关文章

网友评论

      本文标题:2018-11-18

      本文链接:https://www.haomeiwen.com/subject/lkcnfqtx.html