javaweb学习笔记1

1,properties配置文件的使用

  src下创建Bank.properties文件,设置键值对 money = 5000;

  在程序中可以直接拿到money: Integer.parseInt(ResourceBundle.getBundle("Bank").getString("money"))


2.使用注解来替换配置文件。
                                        1.定义一个注解
@Retention(RetentionPolicy.RUNTIME)  //注解何时有效
@Target(ElementType.METHOD)  //注解在哪里有效
public @interface BankInfo {

int maxMoney();
}

2.通过反射来获取注解信息
1.获取当前方法的Method对象。
1.得到Class对象
1.类名.class
2.对象.getClass()
3.Class.forName(String className);
2.得到Method对象
Class.getDeclaredMethod(String                                                                              methodName,Class...paramClass);
2.在Method类中有一个 getAnnotation(Class annotationClass),                                                    可以获取一个注解对象.

3.通过注解对象来调用其属性.

                                         3,部分代码

@BankInfo(MaxMoney=5000)
public void account2(String name, String name1,int money) throws NoSuchMethodException, SecurityException{

Class clazz = this.getClass();    //得到class对象

Method method = clazz.getDeclaredMethod("account2",String.class,String.class,int.class);

BankInfo bif = method.getAnnotation(BankInfo.class);  //通过method得到注解对象

int maxMoney = bif.MaxMoney();


3,获取connection工具类

(1),常规方法

public class JdbcUtils{

  public static connection getConnection(){

        Class.forName("com.mysql.jdbc.Driver");

        String url="";

        String password="";

        Stirng root="";
        Connection con = DriverManager.getConnection(url,root,password);

        return con;

  }

}

(2),注解

@JdbcInfo(driverClassName = "com.mysql.jdbc.Driver", url = "jdbc:mysql:///day24", username = "root", password = "abc")

public static Connection getConnectionByAnnotation() throws Exception {

// 得到当前方法上的注解JdbcInfo
Method method = JdbcUtils.class
.getDeclaredMethod("getConnectionByAnnotation");
JdbcInfo jif = method.getAnnotation(JdbcInfo.class);


String driverClassName = jif.driverClassName();
String url = jif.url();
String username = jif.username();
String password = jif.password();
// 1.加载驱动
Class.forName(driverClassName);
// 2.获取连接
Connection con = DriverManager.getConnection(url, username, password);

return con;
}


4,常规和通过反射调用main方法

public static void main(String[] args) {
// TODO Auto-generated method stub
        System.out.println(args[0]);
}
@org.junit.Test
public void test1() throws NoSuchMethodException, SecurityException, IllegalAccessException, IllegalArgumentException, InvocationTargetException{

//main(new String[]{"tom","jack"});    //常规调用

Method method = this.getClass().getDeclaredMethod("main",String[].class);

method.invoke(null, (Object)(new String[]{"tom","jack"}));//main是静态为null,否则填this;第二个必须强转对象
}


5,注解与xml的联系

  <url-pattern></url-pattern>     String[] urlPatterns() default {};   String[] value() default {};

  <init-param><param-name></param-name></init-param>       WebInitParam[] initParams() default {};

  初始化参数配置@WebServlet(initParams={@WebInitParam(name="username",value="ton")})


6,注解与xml的冲突

   在web.xml文件中的属性 metadata-complete,可以取值为true,false。

   如果为false,代表servlet3.0中的注解可以使用,   如果为true,代表不可以使用注解。


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值