Maven context的简单配置

Spring
  • Spring分为7部分
    • spring Core(基础框架)
    • spring context(上下文)
    • spring DAO(JDBC抽象类和DAO)
    • spring AOP
    • spring web
    • spring ORM(持久层框架 ex:mybatis)
    • spring web MVC
  • Spring三大核心
    • 依赖注入(DI Dependecy Injection)
    • 控制反转(IoC Inversion of Control) 为了高类聚低耦合
    • 面向切面编程(AOP Aspect-Oriented Programming)
    • scope四个值:默认单例
    • java程序两个( prototype,prototype)
    • web程序四个
  • spring context
    • 在maven中创建spring 可以有普通java(quickstark)项目 , 也可以有web(web-app)类
    • 使用java类时 可以使用spring自带tomcat和jdk实现webx项目的功能
    • 然后在main下创建web-app\WEB-INF\web.xml 然后打开Maven\plugins\tomcat7\tomcat7:run 即可
                <packaging>war</packaging>
                <build>
                <plugins>
                <!-- define the project compile level -->
                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-compiler-plugin</artifactId>
                    <version>3.6.1</version>
                    <configuration>
                        <source>1.8</source>
                        <target>1.8</target>
                    </configuration>
                </plugin>

                <!-- 添加tomcat插件 -->
                <plugin>
                    <groupId>org.apache.tomcat.maven</groupId>
                    <artifactId>tomcat7-maven-plugin</artifactId>
                    <version>2.2</version>
                    <configuration>
                        <path>/</path>
                        <port>8888</port>
                    </configuration>
                </plugin>
                </plugins>
                </build>
  • 打开web项目时,先导入spring context依赖
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-context</artifactId>
      <version>4.3.16.RELEASE</version>
    </dependency>
  • 在rosources下 使用 spring config 创建 "beans.xml"文件
    • 中的scope下 默认为 singleton 单例模式(调用同一id创建的对象 相等) 也有prototype(调用同一id创建的对象 不等)
    • 对pojo类中使用IoC(控制反转)时,无参初始化 ,通过set DI(依赖注入)时(如果没有无参构造方法 则报错),写法为
      • 注意:name中内容为对象时,用ref定义,其他用value
    <bean id="home" class="pojos.adress" p:province="湖北" p:city="武汉" p:area="武汉火车站"></bean>
    <bean id="now" class="pojos.adress" p:province="湖北" p:city="武汉" p:area="武昌火车站"></bean>
    <bean id="gf2" class="pojos.girlFriend" scope="singleton">
        <property name="age" value="18"/>
        <property name="look" value="刘亦菲"/>
        <property name="money" value="8000"/>
        <property name="home" ref="home"/>
        <property name="now" ref="now"/>
     </bean>
  • 使用有参构造器时(可以无无参构造器) 有(name + value),(type + value),(index + value),(value),p 四种形式 也可以有参构造和混搭, 一般使用(name +value这种方法 不宜错 可以不按照顺序)
    • 如果有参构造为5个参数,但pojo类没有五个参数的构造器时,则报错
    //p:
    xmlns:p="http://www.springframework.org/schema/p"//要在<beans>中加入这行代码才可以使用p(可随意定义)
    <bean id="home" class="pojos.adress" p:province="湖北" p:city="武汉" p:area="武汉火车站"></bean>
    <bean id="now" class="pojos.adress" p:province="湖北" p:city="武汉" p:area="武昌火车站"></bean>
    <bean id="gf1" class="pojos.girlFriend" p:age="18" p:look="刘亦菲"
    p:money="10000" p:home-ref="home" p:now-ref="now" scope="prototype" >
    </bean>

    // name + value
    <bean id="gf6" class="pojos.girlFriend">
       <constructor-arg name="age" value="18"/>
       <constructor-arg name="look" value="马儿扎哈"/>
       <constructor-arg name="money" value="30000"/>
       <constructor-arg name="now" ref="now"/>
       <constructor-arg name="home" ref="home"/>
    </bean>

    //type + value
    <bean id="gf3" class="pojos.girlFriend">
        <constructor-arg type="int" value="18"/>
        <constructor-arg type="java.lang.String" value="古力娜扎"/>
        <constructor-arg type="double" value="5000"/>
        <constructor-arg type="pojos.adress" ref="now"/>
        <constructor-arg type="pojos.adress" ref="home"/>
    </bean>
    
    //index + value
    <bean id="gf4" class="pojos.girlFriend">
        <constructor-arg index="0" value="18"/>
        <constructor-arg index="1" value="古力娜扎"/>
        <constructor-arg index="2" value="5000"/>
        <constructor-arg index="3" ref="now"/>
        <constructor-arg index="4" ref="home"/>
    </bean>

    //value
    <bean id="gf5" class="pojos.girlFriend">
        <constructor-arg  value="18"/>
        <constructor-arg  value="古力娜扎"/>
        <constructor-arg  value="5000"/>
        <constructor-arg  ref="now"/>
        <constructor-arg  ref="home"/>
    </bean>    
    

千锋逆战班,武汉java逆战1903学习笔记

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
大家好,今天给大家分享一下Android里的Context的一些用法. 这里大致可以分为两种:一是传递Context参数,二是调用全局的Context. 其实我们应用启动的时候会启动Application这个类,这个类是在AndroidManifest.xml文件里其实是默认的 为了让大家更容易理解,写了一个简单的Demo.步骤如下: 第1步:新建一个Android工程ApplicationDemo,目录结构如下: 第2步:新建一个工具类ToolsUtil.java,代码如下 package com.tutor.application; import android.content.Context; import android.widget.Toast; /** * @author carlshen. * 应用的一些工具类. */ public class ToolUtils { /** * 参数带Context. * @param context * @param msg */ public static void showToast(Context context,String msg){ Toast.makeText(context, msg, Toast.LENGTH_SHORT).show(); } /** * 调用全局的Context. * @param msg */ public static void showToast(String msg){ Toast.makeText(MainApplication.getContext(), msg, Toast.LENGTH_SHORT).show(); } } 第3步:新建一个View命名为MainView.java就是我们Activity现实的View.代码如下: package com.tutor.application; import android.app.Activity; import android.content.Context; import android.util.AttributeSet; import android.view.LayoutInflater; import android.view.View; import android.widget.Button; import android.widget.FrameLayout; /** * @author carlshen. * 自定义的MainView. */ public class MainView extends FrameLayout implements View.OnClickListener{ private Context mContext; private Activity mActivity; /** * 参数Button. */ private Button mArgButton; /** * 全局Button. */ private Button mGlobleButton; /** * 退出Button. */ private Button mExitButton; public MainView(Context context){ super(context); setupViews(); } public MainView(Context context, AttributeSet attrs) { super(context, attrs); setupViews(); } private void setupViews(){ //获取View的上下文. mContext = getContext(); //这里将Context转换为Activity. mActivity = (Activity)mContext; LayoutInflater inflater = LayoutInflater.from(mContext); View v = inflater.inflate(R.layout.main, null); addView(v); mArgButton = (Button)v.findViewById(R.id.arg_button); mGlobleButton = (Button)v.findViewById(R.id.glo_button); mExitButton = (Button)v.findViewById(R.id.exit_button); mArgButton.setOnClickListener(this); mGlobleButton.setOnClickListener(this); mExitButton.setOnClickListener(this); } public void onClick(View v) { if(v == mArgButton){ ToolUtils.showToast(mContext, "我是通过传递Context参数显示的!"); }else if(v == mGlobleButton){ ToolUtils.showToast("我是通过全局Context显示的!"); }else{ mActivity.finish(); } } } 这里MainView.java使用的布局main.xml代码如下: <?xml version="1.0" encoding="utf-8"?> 第4步:修改ApplicationDemoActivity.java,代码如下: package com.tutor.application; import android.app.Activity; import android.os.Bundle; public class ApplicationDemoActivity extends Activity { private static Context aContext; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); MainView mMainView = new MainView(this); setContentView(mMainView); aContext = getApplicationContext(); } /**获取Context. * @return */ public static Context getContext(){ return aContext; } } 第5步:运行上述工程效果如下:
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值